Android通知会同时显示并消失,而不做任何事情

Android通知会同时显示并消失,而不做任何事情,android,android-notifications,Android,Android Notifications,我向android手机发送推送通知。 除了三星Galaxy Note 5之外,几乎所有手机都可以接收和显示通知 手机会显示通知图标并同时启动。 您可以在7秒和22秒后看到此问题: 这是我的应用程序的AndroidMenifest.xml <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" and

我向android手机发送推送通知。 除了三星Galaxy Note 5之外,几乎所有手机都可以接收和显示通知

手机会显示通知图标并同时启动。 您可以在7秒和22秒后看到此问题:

这是我的应用程序的AndroidMenifest.xml

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/ic_stat_notification" />
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/colorAccent" />
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_channel_id"
        android:value="@string/default_notification_channel_id" />

    <activity
        android:name=".MainActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name=".FCMInstanceService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>
    <service
        android:name=".MyFirebaseMessagingService"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
</application>
我从哪里开始解决这个问题

-更多-

MyFirebaseMessagingService.java

package com.smartivt.smartivtmessenger;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.Log;

import com.google.firebase.messaging.RemoteMessage;

import java.util.List;

public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {

    private static final String TAG = "FirebaseMessaging";

    private final String CHANNEL_ID = "DEFAULT_CHANNEL";
    private final String CHANNEL_NAME = "Default Channel";
    private final String GROUP_NAME = "MESSAGE_GROUP";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        NotificationManager notiMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        if ( remoteMessage.getData().get("title") != null ) {
            Log.d(TAG, "onMessageReceived: " + remoteMessage.getData().get("title"));
            Log.d(TAG, "onMessageReceived: " + remoteMessage.getData().get("body"));

            if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ) {
                Notification.Builder notify = new Notification.Builder(getApplicationContext(), CHANNEL_ID);

                notify.setSmallIcon(R.drawable.ic_stat_notification);
                notify.setContentTitle(remoteMessage.getData().get("title"));
                notify.setContentText(remoteMessage.getData().get("body"));
                notify.setAutoCancel(true);

                notiMgr.notify(NotificationID.getID(), notify.build());

                /*
                Intent badgeIntent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");

                badgeIntent.putExtra("badge_count", 5);
                badgeIntent.putExtra("badge_count_package_name", getPackageName());
                badgeIntent.putExtra("badge_count_class_name", getLauncherClassName());
                sendBroadcast(badgeIntent);
                */
                Log.d(TAG, "update badge: " + getPackageName() + ", " + getLauncherClassName());
            }
            else {
                Notification.Builder notify = new Notification.Builder(getApplicationContext());

                notify.setSmallIcon(R.drawable.ic_stat_notification);
                notify.setContentTitle(remoteMessage.getData().get("title"));
                notify.setContentText(remoteMessage.getData().get("body"));
                notify.setAutoCancel(true);

                int id = NotificationID.getID();

                Log.d(TAG, "id: " + id);
                notiMgr.notify(id, notify.build());
            }
        }
        else if ( remoteMessage.getNotification() != null ) {
            Log.d(TAG, "onMessageReceived2: " + remoteMessage.getNotification().getTitle());
            Log.d(TAG, "onMessageReceived2: " + remoteMessage.getNotification().getBody());

            if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ) {
                Notification.Builder notify = new Notification.Builder(getApplicationContext(), CHANNEL_ID);

                notify.setSmallIcon(R.drawable.ic_stat_notification);
                notify.setContentTitle(remoteMessage.getNotification().getTitle());
                notify.setContentText(remoteMessage.getNotification().getBody());
                notify.setAutoCancel(true);

                notiMgr.notify(NotificationID.getID(), notify.build());
            }
            else {
                Notification.Builder notify = new Notification.Builder(getApplicationContext());

                notify.setSmallIcon(R.drawable.ic_stat_notification);
                notify.setContentTitle(remoteMessage.getNotification().getTitle());
                notify.setContentText(remoteMessage.getNotification().getBody());
                notify.setAutoCancel(true);

                notiMgr.notify(NotificationID.getID(), notify.build());
            }
        }
    }

    @Nullable
    private String getLauncherClassName () {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        PackageManager pm = getApplicationContext().getPackageManager();
        List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
        for (ResolveInfo resolveInfo : resolveInfos) {
            String pkgName = resolveInfo.activityInfo.applicationInfo.packageName;
            if ( pkgName.equalsIgnoreCase(getPackageName())) {
                return resolveInfo.activityInfo.name;
            }
        }

        return null;
    }
}
public class MyFirebaseMessagingService extends FirebaseMessagingService {
    public static int NOTIFICATION_ID = 1;

    @Override
    public void onNewToken(String s) {
        super.onNewToken(s);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
      //Code to create the Notification

    }
}

您现在不需要两种服务。。。。。 检查此主题:

你不需要这个

<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
渐变型

implementation 'com.google.firebase:firebase-core:16.0.3'
implementation 'com.google.firebase:firebase-messaging:17.3.0'

请使用MyFirebaseMessagingService服务的代码编辑该问题。@Yasiru Nayanajith我添加了它。您不需要AndroidManifest.xml中的FCMInstanceService服务
implementation 'com.google.firebase:firebase-core:16.0.3'
implementation 'com.google.firebase:firebase-messaging:17.3.0'