使用NotificationListener服务时出现两个问题
我在应用程序中使用了NotificationListenerService类。此侦听器#onNotificationPosted方法接收所有通知。
我的代码如下
public class MyListener extends NotificationListenerService
{
public void onNotificationPosted(StatusBarNotification statusBarNotification)
{
//Receive all notification.
// I need to receive reminder notification only
}
//.....
......
}
//My Manifiest like below
<service
android:name=".MyListener"
android:enabled="true"
android:label="@string/app_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService"/>
</intent-filter>
</service>
但是我只需要接收日历提醒通知。
如何添加过滤器以仅接收日历提醒通知?
当我打开通知访问权限时,它将显示以下消息
允许MyApp的通知访问?
MyAPP将能够读取所有通知,包括个人通知
您的联系人姓名和消息文本等信息
接收。它还将能够取消通知或老虎动作
它们包含的按钮
如何在通知访问对话框中更改以上消息?
最佳答案
如何添加过滤器以仅接收日历提醒通知?
要过滤Google的日历,我们可以监听通知
通过使用com.google.android.calendar
程序包名称发布StatusBarNotification.getPackageName
。
为了过滤提醒通知,我们可以监听通知
使用reminder
类别发布,使用Notification.CATEGORY_REMINDER
。 (API 23+)
public class ReminderListenerService extends NotificationListenerService {
private static final String CALENDAR_PKG = "com.google.android.calendar";
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
final Notification noti = sbn.getNotification();
// Filter for the Calendar application
final boolean isCalendar = CALENDAR_PKG.equals(sbn.getPackageName());
// Filter for Notification.CATEGORY_REMINDER (API 23+)
final boolean isReminder = Notification.CATEGORY_REMINDER.equals(noti.category);
// Retrieve the reminder
final String reminder = noti.extras.getString(Notification.EXTRA_TITLE);
}
}
棒糖
就Google日历应用而言,我不确定如何在Lollipop中过滤提醒。使用
Notification.CATEGORY_EVENT
代替NotificationCompat.CATEGORY_REMINDER
之类的东西。如何在通知访问对话框中更改以上消息?
你不能但是您可以显示自己的对话框,解释为什么需要访问,然后使用以下命令将用户定向到通知侦听器设置:
startActivity(new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS)