【问题标题】:Set alarm from notification从通知设置警报
【发布时间】: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


【解决方案1】:

如果您想使用AlarmClock.ACTION_SET_ALARM 设置闹钟,则必须使用PendingIntent.getActvity() 而不是PendingIntent.getBroadcast()AlarmClock.ACTION_SET_ALARMActivity 操作。

如果你不想显示闹钟的 UI,你可以把这个添加到Intent

alarmIntent.putExtra(AlarmClock.EXTRA_SKIP_UI, true); 

【讨论】:

    【解决方案2】:

    当用户点击通知时,您必须在应用中使用广播接收器来接收广播。

    让你的广播接收者是NotifBroadCastReceiver

    public class NotifBroadCastReceiver extends BroadcastReceiver{
        @override
        void onReceive(Context context, Intent intent){
           //you can extract info using intent.getStringExtra or any other method depending on your send data type. After that set alarm here.
        }
    }
    

    所以当你创建待处理的意图时,你可以这样做

    Intent intent = new Intent(context, BroadcastReceiver.class);
    //set all the info you needed to set alarm like time and other using putExtra.
    PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
    

    现在当用户点击通知时,您将在 NotifBroadCastReceiver 的 onReceive 中收到广播。

    注意您必须在清单中注册广播接收器,例如

    <receiver
            android:name="your broadcast receiver"
            android:enabled="true"
            android:exported="false" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多