[英]Notifications in specific time every day android
我想創建一個應用程序讓用戶決定每天的時間讓他通知通知...
我想知道我應該在上午7:00之前在用戶想要的非常時間內觸發通知,並讓他點擊通知並在特定活動中輸入應用程序。 但是,當用戶不想再收到任何通知(點擊按鈕)時,我如何取消所有通知......?
我做了類似的東西
Intent intent = new Intent(this, main.class);
Bundle bundle = new Bundle();
bundle.putString("title", "Some Title");\
intent.putExtras(bundle);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
String body = "Tap to see the Activity";
String title = "Title of notification";
Notification n = new Notification(R.drawable.ic_launcher, body, System.currentTimeMillis());
n.setLatestEventInfo(this, title, body, pi);
n.defaults = Notification.DEFAULT_ALL;
nm.notify(uniqueID, n);
但直到現在還沒有運氣....
謝謝...
<< >>
我這樣做
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.notification_icon;
CharSequence tickerText = "Ome TickerText";
long when = System.currentTimeMillis();
Context context = getApplicationContext();
CharSequence contentTitle = "Some Title";
CharSequence contentText = "Some Text";
Intent notificationIntent = new Intent(context, LandingActivity.class);
Bundle bundle = new Bundle();
bundle.putString("title", "a title");
bundle.putString("title2", "title number 2");
bundle.putString("action", "tip");
bundle.putString("greek", "somehting else");
bundle.putInt("action_num", 2);
notificationIntent.putExtras(bundle);
alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
contentIntent = PendingIntent.getActivity(Notifications.this, 0, notificationIntent, 0);
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notification.defaults = Notification.DEFAULT_ALL;
mNotificationManager.notify(UniqueID, notification);
int hour = tp.getCurrentHour();
int minutes = tp.getCurrentMinute();
contentIntent = PendingIntent.getService(Notifications.this, 0, notificationIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minutes);
calendar.set(Calendar.SECOND, 00);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), contentIntent);
但沒有運氣......
有什么幫助嗎?
使用alarm manager
並將您的通知放在NotifyService
類中
Intent myIntent = new Intent(Current.this , NotifyService.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(ThisApp.this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY , pendingIntent); //set repeating every 24 hours
我知道這是一個老問題,但我有類似的要求,這就是我所做的。 我發現當前系統時間和以毫秒發送通知的預期時間之間的差異,並在android服務中實現了runnable。 以下代碼將出現在Notifyservice中。
private Handler notificationTimer = new Handler() {
@Override
public void handleMessage(Message msg) {
if(msg.what==0) {
sendNotifications();
}
super.handleMessage(msg);
}
};
private Runnable notificationCaller = new Runnable() {
@Override
public void run() {
Message msg = notificationTimer.obtainMessage();
msg.what = 0;
notificationTimer.sendMessage(msg);
}
};
Calendar calendar = Calendar.getInstance();
Calendar timefornotification = Calendar.getInstance();
timefornotification.set(Calendar.HOUR_OF_DAY, 12);
timefornotification.set(Calendar.MINUTE, 30);
timefornotification.set(Calendar.SECOND, 00);
Long difference = calendar.getTimeInMillis() - timefornotification.getTimeInMillis();
if(difference<0){
notificationTimer.postDelayed(notificationCaller, -(difference));
}
else{
notificationTimer.postDelayed(notificationCaller, difference);
}
public void sendNotifications(){
------Your code here-------
notificationTimer.removeCallbacksAndMessages(null);
notificationTimer.postDelayed(notificationCaller, 86400000);
------86400000 is one day. So it will repeat everyday-------
}
然后,您可以從主類調用服務以在后台運行它。
Intent service = new Intent(this, NotifyService.class);
this.startService(service);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.