Simple Notification in Android

Image
Notifications are a way to provide information to your customer which displays as a pop up on the device. It helps you to keep in touch with your customer. I am going to create a simple notification without going deep because everything is available on Google. We must understand a few concepts before going further. Notification channel:  Assume you have developed an app for an online learning center where they teach Maths and English. Students can either learn Maths, English, or Both. we must send notification related to Maths to only students who have taken Maths. In this case, we can create channels for Maths, English, and both so triggered notification will be delivered to students who have taken that subject. Each Notification channel can have a different Notification sound and different behavior that you can change from the Notification setting page. I forgot to tell you that Google has introduced the Notification channel from Android 8.0 i.e API level 26. Now yo...

BootReceiver in android


A boot receiver class is useful when you wish to run a piece of code or start a service as soon as the device has finished booting up. This is very simple to implement requiring only a class that derives from BroadcastReceiver and some configuration in the AndroidManifest.xml file.

So if you need to listen to reboot events in Android there is a simple way:

1. Add permission to RECEIVE_BOOT_COMPLETED

.. code-block:: java
   :linenos:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

2. Add a receiver in the AndroidManifest.xml

.. code-block:: java
   :linenos:

<receiver android:name=".boot">
      <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
      </intent-filter>
    </receiver>

3. Implement a receiver and do implements the necessary method.


    import java.util.Calendar;
public class boot extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
       Intent myIntent = new Intent(context, MyAlarmService.class);

PendingIntent pendingIntent = PendingIntent.getService(context, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 1000);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
       }
     }

Comments

Popular posts from this blog

Webview data directory for Android 9 (Pie)

Tabs in Android

DatePicker in Android