未收到FCM通知

时间:2018-07-11 12:21:22

标签: android firebase-cloud-messaging

我有一个可以接收FCM通知的应用程序。它在达marshmellow的设备上都可以正常运行。当我在oreo设备上安装它时,它会吐司,它表示notificationaton通道为空。在26以上的API上接收通知是必需的。我添加了一个通知通道,但它再次显示了吐司。没有通知。

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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


  }
相关问题