无法从 Android 中的 FirebaseMessagingService 的 onMessageReceived 生成通知

Unable to generate notification from onMessageReceived of FirebaseMessagingService in Android

我发送的通知仅包含来自 Firebase 云消息传递 HTTP 协议的数据负载。我能够在我的 Android 应用程序的 "FirebaseMessagingService" 的 "onMessageReceived" 回调方法中看到通知正确到达。问题是我无法从这里生成通知。我已经编写了用于生成通知的代码,但通知没有出现在我的 phone 的通知区域中。

下面是我的代码:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    String data = remoteMessage.getData().toString();
    Log.d("<<<>>>", "MyFirebaseMessagingService > onMessageReceived > data : " + data);

    String title = remoteMessage.getData().get("title");
    String body = remoteMessage.getData().get("body");

    showNotification(title, body);
}

private void showNotification(String title, String body) {

    Log.d("<<<>>>", "MyFirebaseMessagingService > showNotification() called !");

    Log.d("<<<>>>", "title : " + title);
    Log.d("<<<>>>", "body : " + body);

    Intent intent = new Intent(this, SplashScreenActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "NewsVault_Notification_Channel")
            .setSmallIcon(R.drawable.firebase_notification_icon)
            .setContentTitle(title)
            .setContentText(body)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());
}

}

下面是我的 Logcat 输出:

D/<<<>>>: MyFirebaseMessagingService > onMessageReceived > data : 
{channel_id=NewsVault_Notification_Channel, priority=high, body=Test body, key_1=Value for key_1,     
key_2=Value for key_2, title=Test title, content_available=true}
D/<<<>>>: MyFirebaseMessagingService > showNotification() called !
D/<<<>>>: title : Test title
D/<<<>>>: body : Test body

我在清单文件中添加了以下内容:

<service
        android:name=".Services.MyFirebaseMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

<meta-data
        android:name="com.google.firebase.messaging.default_notification_channel_id"
        android:value="NewsVault_Notification_Channel" />

你在 AndroidManifest.xml 中添加了吗? :

<service android:name="YOURPACKAGE.MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
</service>

对于 Android 版本 >= Oreo(API 级别 26),您必须按如下方式创建通知通道:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("General", "General notification", NotificationManager.IMPORTANCE_DEFAULT);
            channel.setVibrationPattern(new long[]{1000, 1000, 1000, 1000, 1000});

            AudioAttributes attributes = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                    .build();

            channel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI, attributes);
            mNotificationManager.createNotificationChannel(channel);
        }
        if (isAppIsInBackground(context)) {
        int res = Utility.getRandomId();
        PendingIntent pendingIntent;

        Intent intent = setActivityIntent(notifyDataModel);
        intent.putExtra(AppConstants.BOOKING_ID, id);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        pendingIntent = PendingIntent.getActivity(context, res, intent, 0);

        mBuilder.setContentTitle(notifyDataModel.getStrTitle())
                .setContentText(notifyDataModel.getStrMessage())
                .setNumber(1)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
                .setSmallIcon(R.mipmap.ic_launcher)
                .setAutoCancel(true)
                .setColor(ContextCompat.getColor(context, R.color.colorAccent))
                .setContentIntent(pendingIntent);
        getManager().notify(res, mBuilder.build());

    } else {
        int res = Utility.getRandomId();
        PendingIntent pendingIntent;
        Intent intent = setActivityIntent(notifyDataModel);
        intent.putExtra(AppConstants.BOOKING_ID, id);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        pendingIntent = PendingIntent.getActivity(context, res, intent, 0);

        mBuilder.setContentTitle(notifyDataModel.getStrTitle())
                .setContentText(notifyDataModel.getStrMessage())
                .setNumber(1)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
                .setSmallIcon(R.mipmap.ic_launcher)
                .setAutoCancel(true)
                .setColor(ContextCompat.getColor(context, R.color.colorAccent))
                .setContentIntent(pendingIntent);
        getManager().notify(res, mBuilder.build());

    }

PendingIntent pendingIntent = PendingIntent.getActivity(这, 0, 意图, PendingIntent.FLAG_ONE_SHOT);

    String channelId ="100";
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(title)
                    .setContentText("demo")
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId,
                "test",
                NotificationManager.IMPORTANCE_DEFAULT);
        channel.enableLights(true);
        notificationManager.createNotificationChannel(channel);
    }
    notificationManager.notify(0, notificationBuilder.build());