Skip to content Skip to sidebar Skip to footer

Send Notification Id To Receiver While Set Alarm

I try to set an alarm depending user chocies while setting alarm i want to send that alarm's unique notification id to reciever.java. I want to get data by this id on reciever.java

Solution 1:

Here i prepare the alarm manager, every day at same hour

alarmManager = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), MyBroadcastReceiver.class);
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);

// Set the alarm to start at some time.
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        int curHr = calendar.get(Calendar.HOUR_OF_DAY);

        // Checking whether current hour is over 15if (curHr >= 15)
        {
            // Since current hour is over 15, setting the date to the next day
            calendar.add(Calendar.DATE, 1);
        }

        calendar.set(Calendar.HOUR_OF_DAY, 15);
        calendar.set(Calendar.MINUTE, 30);


        // every day
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, pendingIntent);

And here i have the BroadcastReceiver that generate the notification

publicclassMyBroadcastReceiverextendsBroadcastReceiver {

@OverridepublicvoidonReceive(Context context, Intent intent)
{
    PowerManagerpm= (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    //Partial_wake_lock only need CPU active
    PowerManager.WakeLockwl= pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "tag");

    //Acquire the lock
    wl.acquire();

    intmId=0;
    //Show the notification
    NotificationCompat.BuildermBuilder=newNotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.ic_action_edit)
                    .setContentTitle("YOUR APP NAME")
                    .setContentText("TAKE THE PILLS!")
                    .setAutoCancel(true)
                    .setDefaults(-1);

    // Creates an explicit intent for an Activity in your appIntentresultIntent=newIntent(context, MainActivity.class);

        // The stack builder object will contain an artificial back stack for the// started Activity.// This ensures that navigating backward from the Activity leads out of// your application to the Home screen.TaskStackBuilderstackBuilder= TaskStackBuilder.create(context);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(MainActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntentresultPendingIntent=
            stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);

    NotificationManagermNotificationManager=
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mNotificationManager.notify(mId, mBuilder.build());

    //Release the lock
    wl.release();
}
}

The permissions of the manifest

<uses-permissionandroid:name="android.permission.VIBRATE" /><uses-permissionandroid:name="android.permission.WAKE_LOCK" /><uses-permissionandroid:name="com.android.alarm.permission.SET_ALARM" />

And Receiver declaration in the manifest

<receiverandroid:name=".MyBroadcastReceiver" />

For setting your Alarm manager read documentation

If you need to pass data between AlarmManager and BroadcastReceiver use .putExtra() like how this post explain Hope this helps!

Post a Comment for "Send Notification Id To Receiver While Set Alarm"