【问题标题】:Android - Receive calendar reminder notification using NotificationListener serviceAndroid - 使用 NotificationListener 服务接收日历提醒通知
【发布时间】:2017-07-07 00:28:46
【问题描述】:

我在使用 NotificationListener 服务时遇到了两个问题

  1. 我在我的应用程序中使用了 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>

但我只需要接收日历提醒通知。

如何添加过滤器只接收日历提醒通知?

  1. 当我打开通知访问时,它会显示以下消息

    允许 MyApp 访问通知?

    MyAPP 将能够读取所有通知,包括个人通知 联系人姓名和您发送的消息文本等信息 收到。它还可以关闭通知或触发操作 它们包含的按钮

如何在通知访问对话框中更改上述消息?

【问题讨论】:

    标签: java android notifications calendar


    【解决方案1】:

    如何添加过滤器只接收日历提醒通知?

    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)
    

    【讨论】:

    • 感谢@adneal,提醒过滤器 - 根据您的编码,它会在发布通知后检查。在收到通知之前可以这样做吗?
    • 不,这听起来不可能。
    • 非常感谢@adneal
    【解决方案2】:
    <receiver
            android:name=".Calendar.CatchChangesReceiver"
            android:exported="true"
            android:priority="1000">
            <intent-filter>
                <!--   <action android:name="android.intent.action.PROVIDER_CHANGED" />-->
                <action android:name="android.intent.action.EVENT_REMINDER" />
    
                <data android:scheme="content" />
                <data android:host="com.android.calendar" />
            </intent-filter>
        </receiver>
    

    公共类 CatchChangesReceiver 扩展广播接收器 {

    @Override
    public void onReceive(Context context, Intent intent) {
    
        if (intent.getAction().equalsIgnoreCase(CalendarContract.ACTION_EVENT_REMINDER)) {
            //Do Something Here to get EVENT ID
            Uri uri = intent.getData();
            String alertTime = uri.getLastPathSegment();
            String selection = CalendarContract.CalendarAlerts.ALARM_TIME + "=?";
            Cursor curs = context.getContentResolver().query(CalendarContract.CalendarAlerts.CONTENT_URI_BY_INSTANCE, new String[]{CalendarContract.CalendarAlerts.EVENT_ID, CalendarContract.CalendarAlerts.TITLE}, selection, new String[]{alertTime}, null);
    
    
        }
    
    }
    

    }

    【讨论】:

    • 感谢@Happy Singh,是否可以从onReceive 方法获取通知标题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多