当应用程序在后台/销毁时,不会调用Firebase推送通知onMessageReceived

时间:2018-10-11 11:06:39

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

当我的应用程序处于后台并且收到通知时,我没有收到任何数据或意图。

MyFirebaseMessagingService.class:

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.vmegypt.ebsar.Activity.MyOrderDetailsCopyActivity;
import com.vmegypt.ebsar.R;
import com.vmegypt.ebsar.Utils.PrefManager;

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    public static final String CHANNEL_ONE_ID = "com.vmegypt.ebsar";

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

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createChannel();
        }
        Intent intent = new Intent(this, MyOrderDetailsCopyActivity.class);
        String id = remoteMessage.getData().get("PostID");
        intent.putExtra("id", id);
        Log.d("id", "ididididididid: " + id);

        RemoteMessage.Notification iddhee = remoteMessage.getNotification();
        Log.d("TAG", "Message data payload: " + PrefManager.getIdForNotification(MyFirebaseMessagingService.this));

        PendingIntent PendingIntent = android.app.PendingIntent.getActivity(MyFirebaseMessagingService.this, 0, intent, android.app.PendingIntent.FLAG_ONE_SHOT);

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

        NotificationCompat.Builder builder = new NotificationCompat.Builder(MyFirebaseMessagingService.this, CHANNEL_ONE_ID)
                .setSmallIcon(R.drawable.ebsarlogo)
                .setContentTitle(remoteMessage.getNotification().getTitle())
                .setContentText(remoteMessage.getNotification().getBody())
                .setStyle(new NotificationCompat.BigTextStyle())
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setPriority(Notification.PRIORITY_MAX)
                .setContentIntent(PendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0, builder.build());

    }

    // Create Notification Channels To Let Notification Work At Android O
    @RequiresApi(api = Build.VERSION_CODES.O)
    public void createChannel() {

        CharSequence sequence = "Notification";
        String description = "Description Here";
        int importance = NotificationManager.IMPORTANCE_HIGH;

        NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,
                sequence, importance);

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);
    }


}

MyFirebaseInstanceIDService.class:

    package com.vmegypt.ebsar.Helper;

import android.util.Log;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
import com.vmegypt.ebsar.Utils.PrefManager;

    public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

        @Override
        public void onTokenRefresh() {
            super.onTokenRefresh();
            String refreshedToken = FirebaseInstanceId.getInstance().getToken();
            Log.d("TAAAAG", "Refreshed token: " + refreshedToken);

            PrefManager.setDeviceId(MyFirebaseInstanceIDService.this, refreshedToken);

        }

    }

MyFirebaseMessagingService和MyFirebaseInstanceIDService的清单:

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/ebsarlogo" />

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


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

0 个答案:

没有答案