首页 > 解决方案 > 未收到 FCM 通知

问题描述

我有一个应用程序,它将获得 FCM 通知。它在直到 marshmellow 的设备上都可以正常接收。当我在 oreo 设备上安装它时,它得到了 toast,它说通知通道为空。我在谷歌上搜索,我发现接收需要通知通道26 以上 API 上的通知。我添加了一个通知通道,但它再次显示 toast。没有通知。

我的 AppFirebaseMessagingService

public class AppFirebaseMessagingService extends FirebaseMessagingService {


    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {




        String title;
        String description;
        String click_action;
        if(remoteMessage.getData()!=null) {
            title = (remoteMessage.getData().get("title") == null || remoteMessage.getData().get("title").equals("")) ? "null" : remoteMessage.getData().get("title");
            description = (remoteMessage.getData().get("body") == null || remoteMessage.getData().get("body").equals("")) ? "null" : remoteMessage.getData().get("body");
            click_action = (remoteMessage.getData().get("click_action") == null || remoteMessage.getData().get("click_action").equals("")) ? "null" : remoteMessage.getData().get("click_action");


            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                String id = "id_product";
                // The user-visible name of the channel.
                CharSequence name = "Product";
                // The user-visible description of the channel.
                 description = "Notifications regarding our products";
                int importance = NotificationManager.IMPORTANCE_MAX;
                NotificationChannel mChannel = new NotificationChannel(id, name, importance);
                // Configure the notification channel.
                mChannel.setDescription(description);
                mChannel.enableLights(true);
                // Sets the notification light color for notifications posted to this
                // channel, if the device supports this feature.
                mChannel.setLightColor(Color.RED);
                notificationManager.createNotificationChannel(mChannel);
            }



            //Notification----------------------
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(AppFirebaseMessagingService.this);
            mBuilder.setSmallIcon(R.mipmap.ic_launcher);
            mBuilder.setContentTitle(title);
            mBuilder.setContentText(description);
            Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            mBuilder.setSound(alarmSound);
            SharedPreferences sharedpreferences = getSharedPreferences(Common.preferenceName, Context.MODE_PRIVATE);
            String RoleCSV=sharedpreferences.getString(Common.roleCSV,"");




        }
    }
}

我的 Android 清单

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <service android:name=".AppFirebaseInstanceIdService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>
    <service android:name=".AppFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
    <activity
        android:name=".Login"
        android:configChanges="orientation|screenSize"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.Design.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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


      <activity
        android:name=".HomeScreen"
        android:configChanges="orientation|screenSize" />
    <activity

</application>

标签: androidfirebase-cloud-messaging

解决方案


我写了一段代码来检查设备是否是奥利奥

    String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
    NotificationManager notificationManager1 = (NotificationManager) 
    getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

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

        // Configure the notification channel.
        notificationChannel.setDescription("Channel description");
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
        notificationChannel.enableVibration(true);
        notificationManager1.createNotificationChannel(notificationChannel);
    }
     mNotification = builder
                .setLargeIcon(image)/*Notification icon image*/
                .setContentText(messageBody)
                .setSmallIcon(R.drawable.dhlone)
                .setContentTitle(title)
                .setStyle(new NotificationCompat.BigPictureStyle()
                .bigPicture(image).bigLargeIcon(image))/*Notification with Image*/
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent)
                .setBadgeIconType(R.drawable.dhlone)
                .setAutoCancel(true)
                .setSmallIcon(getNotificationIcon()) 
                .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext()
                .getResources(),R.drawable.dhlone))
                .build();
                notificationManager1.notify(/*notification id*/0, mNotification);

推荐阅读