local notification not repeating

你离开我真会死。 提交于 2020-01-05 09:13:20

问题


I need to show notification for completing registration to user in every 2 hrs , I have set it to few mins for testing , but my notification comes only once it never come again . Please suggest.

My code for repeating task :

public void createNotification() {
    Intent notificationIntent = new Intent(context, ShowNotification.class);
    PendingIntent contentIntent = PendingIntent.getService(context, 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    //  am.cancel(contentIntent);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 60); // first time
    long frequency= 60 * 1000; // in ms

    am.setRepeating(AlarmManager.RTC,  calendar.getTimeInMillis(), frequency, contentIntent);


}

My Code in service :

public class ShowNotification extends Service {

private final static String TAG = "ShowNotification";

@Override
public void onCreate() {
    super.onCreate();

   Intent mainIntent = new Intent(this, DashBoardActivity.class);

    NotificationManager notificationManager
            = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

    Notification noti = new NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentIntent(PendingIntent.getActivity(this, 0, mainIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT))
            .setContentTitle("HELLO " + System.currentTimeMillis())
            .setContentText("Please complete all the steps for registration")
            .setDefaults(Notification.DEFAULT_ALL)
            .setSmallIcon(R.drawable.ic_launcher)
            .setTicker("ticker message")
            .setWhen(System.currentTimeMillis())
            .build();

    notificationManager.notify((int) System.currentTimeMillis(), noti);
   // ToastUtil.displayToast(this , "my msg");
    Log.i(TAG, "Notification created");
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

}


回答1:


You need to override onStartCommand of Service and Move your code of onCreate there. Like below

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // And move all codes of onCreate here


Intent mainIntent = new Intent(this, DashBoardActivity.class);

NotificationManager notificationManager
        = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

Notification noti = new NotificationCompat.Builder(this)
        .setAutoCancel(true)
        .setContentIntent(PendingIntent.getActivity(this, 0, mainIntent,
                PendingIntent.FLAG_UPDATE_CURRENT))
        .setContentTitle("HELLO " + System.currentTimeMillis())
        .setContentText("Please complete all the steps for registration")
        .setDefaults(Notification.DEFAULT_ALL)
        .setSmallIcon(R.drawable.ic_launcher)
        .setTicker("ticker message")
        .setWhen(System.currentTimeMillis())
        .build();

notificationManager.notify((int) System.currentTimeMillis(), noti);
// ToastUtil.displayToast(this , "my msg");
Log.i(TAG, "Notification created");



    // We want this service to continue running until it is explicitly
    // stopped, so return sticky.
    return START_STICKY;
}

Read more about lifecycle of Service.




回答2:


PendingIntent contentIntent = PendingIntent.getService(DashBoardActivity.this, 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

instead try using

 Intent notificationIntent = new Intent(mContext, Service.class);
 notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 PendingIntent contentIntent = PendingIntent.getService(myContext, 0, notificationIntent, 0);



回答3:


if (!isMyServiceRunning(trachservice.class)) {
                Intent intents = new Intent(this, trachservice.class);
            PendingIntent pendingIntent = PendingIntent.getService(this, 12345,
                    intents, PendingIntent.FLAG_CANCEL_CURRENT);
            AlarmManager am = (AlarmManager) this
                    .getSystemService(Activity.ALARM_SERVICE);
            try {
                // am.cancel(pendingIntent);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            am.setRepeating(AlarmManager.ELAPSED_REALTIME,
                    SystemClock.elapsedRealtime(), 300000, pendingIntent);
        } else {

            Intent intents = new Intent(this, trachservice.class);
            this.stopService(intents);

            PendingIntent pendingIntent = PendingIntent.getService(this, 12345,
                    intents, PendingIntent.FLAG_CANCEL_CURRENT);
            AlarmManager am = (AlarmManager) this
                    .getSystemService(Activity.ALARM_SERVICE);
            try {
                // am.cancel(pendingIntent);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            am.setRepeating(AlarmManager.ELAPSED_REALTIME,
                    SystemClock.elapsedRealtime(), 300000, pendingIntent);
        }

Write a Service put Your Notification Code under .. if you want to use in your Activity Your can also call Handler of Your Class


来源:https://stackoverflow.com/questions/28559458/local-notification-not-repeating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!