Android未显示API 21-25的Firebase通知

时间:2018-06-28 13:36:27

标签: android firebase push-notification android-notifications

我正在开发具有Firebase通知的android应用

android {
compileSdkVersion 27
defaultConfig {
    applicationId "my.package"
    minSdkVersion 21
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
有关API和设备的

通知如下:

  • 具有API 22的真实设备和仿真器:未显示通知
  • 使用API​​ 24的模拟器:不显示通知
  • 使用API​​ 27的模拟器:可以,显示通知

,这里是我处理通知的示例代码:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Log.w("fcm", "received notification");
    if (remoteMessage.getNotification() != null && remoteMessage.getNotification().getTitle() != null && remoteMessage.getNotification().getBody() != null) {
        sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
    }
}

private void sendNotification(String messageTitle, String messageBody) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent;

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this).setTicker(messageTitle).setWhen(0).setSmallIcon(R.drawable.logo).setChannelId("2017").setAutoCancel(false).setContentTitle(messageTitle).setContentText(messageBody).setSound(defaultSoundUri).setPriority(Notification.PRIORITY_MAX).setContentIntent(pendingIntent).setDefaults(Notification.DEFAULT_ALL);

        NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
        bigTextStyle.setBigContentTitle(messageTitle);
        bigTextStyle.bigText(messageBody);
        notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody));

        NotificationManager notificationManager = (NotificationManager) getSystemService(this.NOTIFICATION_SERVICE);
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = notificationManager.getNotificationChannel("2017");
        if (mChannel == null) {
            mChannel = new NotificationChannel("2017", "TEST", importance);
            mChannel.setDescription("test notification");
            mChannel.enableVibration(true);
            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            notificationManager.createNotificationChannel(mChannel);
        }
        notificationManager.notify(1, notificationBuilder.build());
    } else {
        Notification notification = new Notification.Builder(this).setCategory(Notification.CATEGORY_MESSAGE)
                .setContentTitle(messageTitle).setContentText(messageBody).
                        setSmallIcon(R.drawable.logo).setAutoCancel(true).
                        setVisibility(Notification.VISIBILITY_PUBLIC).build();
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(2, notification);
    }
}

可能是什么问题?

0 个答案:

没有答案