0

我正在使用FCM,并根据文档“通知传递到设备的系统托盘”。那么当通知消息被用户解散时,如何附加挂起的意图或注册广播/回叫?如何在用户解散fcm通知消息时获得广播或回拨?

有没有办法做到这一点?或者我必须使用数据消息并手动建立通知,并且在通知被解除的情况下等待接收广播的意图?

编辑 - 解决方案

public class NotificationListener extends NotificationListenerService { 
    private boolean isSafeToExecute, isNotificationAccessEnabled; 



    @Override 
    public void onNotificationPosted(StatusBarNotification sbn) { 
      super.onNotificationPosted(sbn); 
    } 

    @Override 
    public void onNotificationRemoved(StatusBarNotification sbn) { 
      Log.e("removed", sbn.getNotification().getClass().getSimpleName()); 
    } 

    @Override 
    public void onListenerConnected() { 
     super.onListenerConnected(); 
     isSafeToExecute = true; 
    } 

    @Override 
    public void onListenerDisconnected() { 
     super.onListenerDisconnected(); 
     isSafeToExecute = false; 
    } 
    @Override 
    public IBinder onBind(Intent mIntent) { 
     IBinder mIBinder = super.onBind(mIntent); 
     isNotificationAccessEnabled = true; 
     return mIBinder; 
    } 

    @Override 
    public boolean onUnbind(Intent mIntent) { 
     boolean mOnUnbind = super.onUnbind(mIntent); 
     isNotificationAccessEnabled = false; 
     return mOnUnbind; 
    } 
} 

上述作品之一。我以前没有覆盖导致问题的onBind和OnUnbind。 如果未授予,请求用户授予通知访问权限,此链接将有所帮助。 Check if user has granted NotificationListener access to my app

回答

1

你应该与onNotificationRemoved(StatusBarNotification SBN)去

这将让你在用户已删除的通知或者Andorid的系统已经撤回该通知的情况下追上。

+0

嗨,你有没有用FCM通知消息NotificationListenerService? – Debanjan

+0

是的,请查看此链接了解更多详情,https://developer.android.com/reference/android/service/notification/NotificationListenerService.html –

+0

我已经尝试过了,但是当fcm通知被解雇时我没有收到通知或onNotificationPosted。我们是否必须使用startService()手动启动服务,否则意图会执行此操作? 我已经完成了链接。 还有一件事,当我去设置 - >安全 - >通知访问, 它没有显示我的应用程序,而是显示 “没有应用程序在此设备上能够读取通知” – Debanjan