Firebase-应用程序被杀死时未收到推送通知-Android 8

时间:2020-10-05 05:21:08

标签: android firebase push-notification firebase-cloud-messaging android-8.0-oreo

我想开发以上版本的android 8应用程序,当我的应用程序被杀死时,我无法收到通知。但是,它在下面的android 7或打开的应用程序或在后台的应用程序中都可以正常工作。 我的应用程序被杀死时,如何仍能收到通知?谢谢

我的代码:

AndroidManifest.xml

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

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

<service
    android:name=".firebase.MyFirebaseMessagingService"
    android:enabled="true"
    android:exported="true"
    android:permission="com.google.android.c2dm.permission.SEND">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
    </intent-filter>
</service>

FirebaseMessaging.java

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    if (remoteMessage == null)
        return;

    if (remoteMessage.getData()!= null)
        try {
            Map<String, String> data = remoteMessage.getData();

            PushNotificationModel pushNotificationModel = PushNotificationModel.fromMap(data);
            Intent resultIntent = new Intent(getApplicationContext(), SplashActivity.class);

            showNotificationMessage(getApplicationContext(), pushNotificationModel.title, pushNotificationModel.message,  pushNotificationModel.timestamp, resultIntent, Integer.parseInt(pushNotificationModel.notifid));
        } catch (Exception e) {
            e.printStackTrace();
        }
}

public void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent , int notifid)
{
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    int notificationId = notifid;
    String channelId = "channel-01";
    String channelName = "Channel Name";
    int importance = NotificationManager.IMPORTANCE_HIGH;

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel mChannel = new NotificationChannel(
                channelId, channelName, importance);
        notificationManager.createNotificationChannel(mChannel);
    }

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
            .setSmallIcon(R.drawable.logo)
            .setContentTitle(title)
            .setContentText(message)
            .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText(message));

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addNextIntent(intent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
            0,
            PendingIntent.FLAG_UPDATE_CURRENT
    );
    mBuilder.setContentIntent(resultPendingIntent);

    notificationManager.notify(notificationId, mBuilder.build());
}

邮递员发送通知(此正文,在标题处,我具有授权码和发件人ID)

{
    "to": "token",
    "priority":"high",
    "data": {
        "message": "Henlo",
        "notifid": 2020090001,
        "timestamp": "2020-09-29 17:45:22",
        "title": "Hai"
    }
}

谢谢:)

0 个答案:

没有答案