【问题标题】:local notification not repeating本地通知不重复
【发布时间】:2015-02-17 10:09:22
【问题描述】:

我需要每 2 小时向用户显示一次完成注册的通知,我已将其设置为几分钟以进行测试,但我的通知只会出现一次,它再也不会出现。 请提出建议。

我的重复任务代码:

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);


}

我的服务代码:

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;
}

}

【问题讨论】:

  • AlarmManager.RTC_WAKEUP代替AlarmManager.RTC试试
  • DashBoardActivity 是服务类还是活动?分享代码。
  • 这是一个 Activity ,只是将上下文传递到那里
  • 添加了答案。删除您的 onCreate 代码并添加到 onStartCommand

标签: android notifications alarmmanager repeatingalarm


【解决方案1】:

您需要覆盖 onStartCommand 的 Service 并将您的 onCreate 代码移到那里。如下所示

@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;
}

阅读更多关于lifecycle of Service的信息。

【讨论】:

    【解决方案2】:
    PendingIntent contentIntent = PendingIntent.getService(DashBoardActivity.this, 0, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
    

    改为尝试使用

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

    【讨论】:

    • Intent.FLAG_ACTIVITY_NEW_TASK not accepted 它说它必须是 PendingIntent
    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2020-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-07
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多