Skip to content Skip to sidebar Skip to footer

How To Show Local Notification Every Hour Using Service

I want to show local notification on every hour or particular seconds using service, I have tried to implement this functionality, but I didn't got success. My code is look like be

Solution 1:

Your code seems to be OK. The only thing that can be problematic is the interval 4*60*60 is too short which is 14.4 seconds.

Moreover it seems like you're not directing the intent to a specific receiver. you should do like this:

Calendarcalendar= Calendar.getInstance();
Intentintent1=newIntent(MainActivity.this, AlarmReceiver.class);
PendingIntentpendingIntent= PendingIntent.getBroadcast(this, 0,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManageram= (AlarmManager)this.getSystemService(this.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 60, pendingIntent);

And you should catch it in:

publicclassAlarmReceiverextendsBroadcastReceiver{

@OverridepublicvoidonReceive(Context context, Intent intent) {
    // TODO Auto-generated method stublongwhen= System.currentTimeMillis();
    NotificationManagernotificationManager= (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    IntentnotificationIntent=newIntent(context, EVentsPerform.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntentpendingIntent= PendingIntent.getActivity(context, 0,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


    NotificationCompat.BuildermNotifyBuilder=newNotificationCompat.Builder(
            context).setSmallIcon(R.drawable.applogo)
            .setContentTitle("text")
            .setContentText("text")
            .setWhen(when)
            .setContentIntent(pendingIntent)
    notificationManager.notify(yourId, mNotifyBuilder.build());

}

}

Add this to your manifest file:

<!-- permission required to use Alarm Manager --><uses-permissionandroid:name="com.android.alarm.permission.SET_ALARM"/><!-- Register the Alarm Receiver --><receiverandroid:name="com.example.alarmmanagernotifcation.AlarmReceiver"/>

Post a Comment for "How To Show Local Notification Every Hour Using Service"