Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android GetActiveNotifications返回null_Android_Firebase_Push Notification_Firebase Cloud Messaging_Android Service - Fatal编程技术网

Android GetActiveNotifications返回null

Android GetActiveNotifications返回null,android,firebase,push-notification,firebase-cloud-messaging,android-service,Android,Firebase,Push Notification,Firebase Cloud Messaging,Android Service,我有一个messenger应用程序,现在我正在做推送通知部分。为此,我使用了FCM(Firebase云消息传递)。现在我有一个问题。例如,引入2个用户(user1和user2)。如果user1写信给我,我将收到FCM的通知并显示它。当user2给我写信时,我正在为它创建新的通知。但是,当user1再次给我写信时,我没有删除我的user1通知,我需要更新通知,否则创建新的。我想你理解我了。现在我被困在更新部分,因为每次我创建一个关于消息收入的新通知时 我搜索了一下,发现了一些东西。它说我必须使用N

我有一个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());
    }
}

您不需要
通知ListenerService
。只需从
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
    }
}

您不需要
通知ListenerService
。只需从
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
    }
}

亲爱的朋友。notificationmanager.getActiveNotifications()需要API 23及更高版本。我的最小API是18。知道吗?亲爱的朋友。notificationmanager.getActiveNotifications()需要API 23及更高版本。我的最小API是18。有什么想法吗?你能添加通知创建部分吗?好的,我现在就添加。你能添加通知创建部分吗?好的,我现在就添加。