GetActiveNotifications返回null

时间:2019-03-11 09:43:06

标签: android firebase push-notification firebase-cloud-messaging android-service

//重要提示-没有重复,没有帮助我,所以我有所有权限问我的问题。谢谢。

嗨。我有一个Messenger应用程序,现在正在做推送通知部分。我为此使用了FCM(Firebase云消息传递)。现在我有一个问题。例如,介绍2个用户(user1和user2)。如果user1给我写信,我将收到FCM的通知并显示它。当user2给我写信时,我正在为其创建新的通知。但是当user1再次写信给我并且我没有删除user1通知时,我需要更新通知,否则创建新的通知。我想你了解我。现在,我停留在更新部分,因为每次我都创建有关消息收入的新通知时。

我搜索并发现了一些东西。它说我必须使用NotificationListenerService并使用getActiveNotifications()来获取状态栏中的所有活动通知。

因此,我尝试了一些操作,但得到null。我在Notification Access中启用了此应用程序,因此我认为这不是问题。 我在清单文件中注册了服务。

这是我的代码,我在其中创建了FCM服务。如果我在代码中做错了什么,请修复它。

public class FireBaseService extends FirebaseMessagingService {

private static final String TAG = "FireBaseMessagingServiceTag";

@SuppressLint("LongLogTag")
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // Handle FCM messages here.

    Intent service = new Intent(FireBaseService.this, NotificationService.class);
    startService(service);

    //I think I'm doing wrong here.
    NotificationService notificationService = new NotificationService();

    StatusBarNotification[] activeNotifications = notificationService.getActiveNotifications();

    Log.d(TAG, "Array: " + Arrays.toString(activeNotifications));

    //show notification
}
}

这是NotificationService类。

public class NotificationService extends NotificationListenerService {

@Override
public IBinder onBind(Intent intent) {
    return super.onBind(intent);
}

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

@Override
public void onNotificationPosted(StatusBarNotification sbn){
    // Implement what you want here
}

@Override
public void onNotificationRemoved(StatusBarNotification sbn){
    // Implement what you want here
}

@Override
public StatusBarNotification[] getActiveNotifications() {
    return super.getActiveNotifications();
}
}

这就是我所做的。你能帮助我吗?谢谢,如果我做错了事而不是对我的问题投了反对票,请修正我的代码。

这是通知部分。

private void showNotification(String title, String body) {
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    String NOTIFICATION_CHANNEL_ID = "com.example.fcmnotification.FireBaseService";

    if(notificationManager != null) {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(
                    NOTIFICATION_CHANNEL_ID, "NotificationManager",
                    NotificationManager.IMPORTANCE_DEFAULT);

            notificationChannel.setDescription("My Channel");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.BLUE);
            notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
            notificationManager.createNotificationChannel(notificationChannel);
        }

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
        notificationBuilder.setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher_notification)
                .setContentTitle(title)
                .setContentText(body)
                .setContentInfo("info");

        notificationManager.notify(new Random().nextInt(), notificationBuilder.build());
    }
}

1 个答案:

答案 0 :(得分:0)

您不需要NotificationListenerService。只需从NotificationManager

获取活动通知
NotificationManager nm = (NotificationManager)ctx.getSystemService(Activity.NOTIFICATION_SERVICE);
StatusBarNotification[] statusNotifs = null;
try {
    statusNotifs = nm.getActiveNotifications();
} catch (Throwable t) {
    // it can crash
}
if (statusNotifs != null) for (StatusBarNotification n : statusNotifs) {
    Notification notif = n.getNotification();
    try {
        String ntext = notif.extras.getCharSequence(Notification.EXTRA_TEXT).toString();
        // do something
    } catch (Throwable t) {
        // can crash
    }
}
相关问题