Thirsty
Thirsty

Reputation: 317

Set a periodic notification in android

i am trying to create a periodic notification in android, and i have almost achieved it, the problem is that with my code it opens the app instead of creating the notification, here is my code:

NotificationCompat.Builder mBuilder =
           new NotificationCompat.Builder(this)
                   .setSmallIcon(R.mipmap.ic_launcher)
                   .setContentTitle(this.getText(R.string.notificationMessage))
                   .setContentText("")
                   .setAutoCancel(true);

    Intent resultIntent = new Intent(this, MainActivity.class);

    // Because clicking the notification opens a new ("special") activity, there's
    // no need to create an artificial back stack.
    PendingIntent resultPendingIntent =
           PendingIntent.getActivity(
                   this,
                   0,
                   resultIntent,
                   PendingIntent.FLAG_UPDATE_CURRENT
           );
    mBuilder.setContentIntent(resultPendingIntent);

    NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    mNotifyMgr.notify(1,mBuilder.build());


    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 07);
    calendar.set(Calendar.MINUTE, 00);
    calendar.set(Calendar.SECOND, 00);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, resultPendingIntent);  //set repeating every 24 hours
    

UPDATED CODE:

class Main Activity:

PendingIntent servicePendingIntent = PendingIntent.getService(this, 0, new Intent(this, NotificationService.class), PendingIntent.FLAG_CANCEL_CURRENT );

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 15);
    calendar.set(Calendar.MINUTE, 20);
    calendar.set(Calendar.SECOND, 00);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, servicePendingIntent);  //set repeating every 24 hours
    

class NotificationService:

public class NotificationService extends IntentService {

    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     * @param name Used to name the worker thread, important only for debugging.
     */
    public NotificationService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        Log.d("NotificationService", "onHandleIntent");

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle(this.getText(R.string.notificationMessage))
                        .setContentText("")
                        .setAutoCancel(true);

        Intent resultIntent = new Intent(this, MainActivity.class);

        // Because clicking the notification opens a new ("special") activity, there's
        // no need to create an artificial back stack.
        PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        this,
                        0,
                        resultIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);

        NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        mNotifyMgr.notify(1,mBuilder.build());



    }

}

Upvotes: 0

Views: 3323

Answers (2)

Curtis Murphy
Curtis Murphy

Reputation: 304

You're passing the pending intent for the activity into the alarm manager. You'll need to move the notification code to a service (I'd recommend an IntentService).

Then you can need to create a pending intent that runs that service and pass that pending intent into the alarm manager. This way every time the alarm goes off, your notification code is run.

PendingIntent servicePendingIntent = PendingIntent.getService(this, 0, new Intent(this, NotificationService.class));
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, servicePendingIntent);

Upvotes: 1

Androiderson
Androiderson

Reputation: 17083

Your PendingIntent points to your activity. You just need to use PendingIntent.getService instead, and have this service showing your notification.

Upvotes: 0

Related Questions