Search code examples
androidpush-notificationalarmmanager

Adding notification using alarm in android


I have two activities. In the first activity, I have set alarm. Now, I want to trigger after 5 seconds and start the second activity. The second activity pushes the notification. For the purpose, I wrote the following code:

Adding alarm Class :

public class NotificationClass extends ActionBarActivity
{
    AlarmManager am;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification_class);
        am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        Intent in = new Intent(this,PushNotification.class);
        //b. create pending intent
        PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(),0,in,0);

        //c. set alarm for 5 seconds.
        am.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis() + 5000,pi);
    }


}

The class which adds notifications:

public class PushNotification extends ActionBarActivity
{
    int notificationID = 11037;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_push_notification);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

        mBuilder.setSmallIcon(R.mipmap.ic_launcher);
        mBuilder.setContentTitle("Notification Alert, Click Me!");
        mBuilder.setContentText("Hi, This is Android Notification Detail!");

        Intent resultIntent = new Intent(this, NotificationClass.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(NotificationClass.class);

// Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);

        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mBuilder.setAutoCancel(true);
// notificationID allows you to update the notification later on.
        mNotificationManager.notify(notificationID, mBuilder.build());
    }

}

But, I am not getting the desired result. I think I have some issue in alarm class. Please help me to solve this. What do I need in android manifest file?


Solution

  • You need to subclass BroadcastReceiver and register it either statically in the manifest file or dynamically using registerReceiver via LocalBroadcastManager. The onReceive() method of your BroadcastReceiver will push the notification when it receives the intent generated by the alarm.

    Static registration is simple:

    <receiver android:name=".MyAlarmReceiver" />
    

    Then move the notify code from your PushNotification.onCreate() method to the MyAlarmReceiver.onReceive() method.

    See BroadcastReceiver or BroadcastReceiver tutorial.

    Hope it helps.