FCM notification not receiving

2019-09-20 10:45发布

问题:

I have an application which will get FCM notifications.It recived fine on devices up to marshmellow.When I insatlled it on oreo device it getting toast which says notificaton channel is null.I searched on google and I found that Notification channels are required for receiving notifications on API above 26. I added a notification channel but it shows the toast again.No notification.

My 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,"");




        }
    }
}

My Androidmanifest

<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>

回答1:

I write the piece of code for checking if the device is oreo

    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);


回答2:

private fun createNotification(title: String, message: String) {
    val resultIntent = Intent(this, LoginActivity::class.java)
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

    val resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT)

    val mBuilder =  NotificationCompat.Builder(this)
    mBuilder.setSmallIcon(R.drawable.ic_stat_name)
    mBuilder.setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(false)
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
            .setContentIntent(resultPendingIntent)

    val mNotificationManager = this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
    {
      val importance = NotificationManager.IMPORTANCE_HIGH
      val notificationChannel = NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
      notificationChannel.enableVibration(true)
      notificationChannel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
      mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID)
      mNotificationManager.createNotificationChannel(notificationChannel);
    }

    mNotificationManager.notify(0 /* Request Code */, mBuilder.build());


  }