【发布时间】:2019-10-21 10:03:11
【问题描述】:
我需要向移动时钟应用程序添加一个闹钟,我将为此向用户发送通知。当用户单击通知时,应在给定时间添加一个新警报。 下面是代码:
//Create intent
Intent alarmIntent = new Intent(AlarmClock.ACTION_SET_ALARM);
alarmIntent.putExtra(AlarmClock.EXTRA_MESSAGE, event.getEventName());
Calendar alarmTime = new GregorianCalendar();
alarmTime.setTime(new Date(event.getAlarmTime()));
alarmIntent.putExtra(AlarmClock.EXTRA_HOUR, alarmTime.get(Calendar.HOUR_OF_DAY));
alarmIntent.putExtra(AlarmClock.EXTRA_MINUTES, alarmTime.get(Calendar.MINUTE));
PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
//Create and show notification
NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel("MyAppsAlarm",
"MyAppsAlarmNotifications",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("Channel to show notifs");
mNotificationManager.createNotificationChannel(channel);
NotificationCompat.Builder builder = new NotificationCompat.Builder(main.getApplicationContext(), "Zzzzz")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Alarm Helper")
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(alarmPendingIntent);
mNotificationManager.notify(0, builder.build());
当我点击通知时,没有任何反应。当通知抽屉自动关闭时,通知保持原样。
我尝试使用startActivity(alarmIntent); 触发intent,它按预期工作,但通知.setContentIntent(alarmPendingIntent); 似乎什么也没做。
【问题讨论】:
-
你必须在你的 PendingIntent.getBroadcast() 中使用明确的意图。
-
你能解释一下吗?
标签: android notifications android-pendingintent alarm