本地通知不重复 [英] local notification not repeating
本文介绍了本地通知不重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要向用户显示每2小时完成一次注册的通知,我将其设置为几分钟,以便进行测试,但是我的通知只有在不再发送时才会出现. 请提出建议.
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.
我的重复任务代码:
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的代码移到那里.如下所示
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;
}
详细了解服务的生命周期.
这篇关于本地通知不重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文