簡體   English   中英

本地通知不重復

[英]local notification not repeating

我需要向用戶顯示每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;
}

}

您需要重寫Service的onStartCommand並將onCreate的代碼onStartCommand那里。 像下面

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

閱讀有關服務生命周期的更多信息。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM