打开应用时没有显示接收到 FCM 通知的应用。

16

当我从Firebase发送推送消息时,如果应用程序在后台或关闭状态下,我会收到通知,但是当应用程序处于打开状态时,通知不显示...

调试后我发现停留在MyMessagingService的onMessageReceived方法上,所以我猜测我的问题可能与生成通知(或者随通知一起发送的意图)有关。

我已经实现了这个服务:

public class MyMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyMessagingService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
    Log.d(TAG, "From: " + remoteMessage.getFrom());

    if(remoteMessage!=null)
    {
        String id = null;
        String title = null;
        String message = null;
        String launchPage = null;

        if(remoteMessage.getNotification()!=null)
        {
            if(remoteMessage.getNotification().getTitle()!=null)
            {
                title = remoteMessage.getNotification().getTitle();
            }

            if(remoteMessage.getNotification().getBody()!=null)
            {
                message = remoteMessage.getNotification().getBody();
            }
        }

        if(remoteMessage.getMessageId()!=null)
        {
            id = remoteMessage.getMessageId();
        }

        Log.e(TAG, "id: " + id);
        Log.e(TAG, Consts.TITLE + title);
        Log.e(TAG, Consts.MESSAGE + ": " + message);

        // Check if message contains a data payload.
        if (remoteMessage.getData()!=null && remoteMessage.getData().size() > 0)
        {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());

            if(remoteMessage.getData().containsKey(Consts.LAUNCH_PAGE))
            {
                launchPage = remoteMessage.getData().get(Consts.LAUNCH_PAGE);
            }

            Log.e(TAG, Consts.LAUNCH_PAGE + ": " + launchPage);

            sendNotification(title, message, launchPage);

        }
    }
}
// [END receive_message]

/**
 * Create and show a simple notification containing the received FCM message.
 *
 * @param messageBody FCM message body received.
 */
private void sendNotification(String title, String messageBody, String page)
{
    Intent intent = new Intent(this, MainActivity.class);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    intent.putExtra(Consts.TITLE, title);
    intent.putExtra(Consts.MESSAGE, messageBody);
    if (launchPage != null)
    {
        intent.putExtra(Consts.LAUNCH_PAGE, launchPage);
    }

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);

    String channelId = getString(R.string.default_notification_channel_id);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.drawable.status)
            .setContentTitle(title)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
} }

在我的清单文件(manifest)中,我有:

 <!-- [START fcm_default_icon] -->
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/status" />

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/accent" />
    <!-- [END fcm_default_icon] -->
    <!-- [START fcm_default_channel] -->
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_channel_id"
        android:value="@string/default_notification_channel_id"/>
    <!-- [END fcm_default_channel] -->

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

    <service
        android:name=".controller.service.MyInstanceIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>

有什么建议可以在应用程序在前台时生成通知吗?


请查看此处的答案:https://stackoverflow.com/questions/48301350/android-notifications-when-app-is-in-background/48301893#48301893 - Peter Haddad
谢谢彼得,但这是一个不同的问题... - ziniestro
5个回答

13

修改你的 MyFirebaseMessagingService 代码,

public class MyFirebaseMessagingService extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message"));

        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        String channelId = "Default";
        NotificationCompat.Builder builder = new  NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(remoteMessage.getNotification().getTitle())
                .setContentText(remoteMessage.getNotification().getBody()).setAutoCancel(true).setContentIntent(pendingIntent);;
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
            manager.createNotificationChannel(channel);
        }
        manager.notify(0, builder.build());
     }
 }

查看 - 如何处理 Firebase 通知当应用程序在前台时


1

我终于解决了问题,原因是我创建通知的方式有问题,这是我的代码:

private void sendNotification(String title, String messageBody, String page)
{
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(), getString(R.string.default_notification_channel_id));
    Intent intent = new Intent(getApplicationContext(), MainActivity.class);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra(Consts.TITLE, title);
    intent.putExtra(Consts.MESSAGE, messageBody);

    if (page != null)
    {
        intent.putExtra(Consts.LAUNCH_PAGE, page);
    }

    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

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

    NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
    bigText.bigText(title);
    bigText.setBigContentTitle(messageBody);

    mBuilder.setContentIntent(pendingIntent);
    mBuilder.setSmallIcon(R.drawable.status);
    mBuilder.setContentTitle(title);
    mBuilder.setContentText(messageBody);
    mBuilder.setPriority(Notification.PRIORITY_MAX);
    mBuilder.setAutoCancel(true);
    mBuilder.setStyle(bigText);

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

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
    {
        NotificationChannel channel = new NotificationChannel(getString(R.string.default_notification_channel_id), getString(R.string.default_notification_channel_name), NotificationManager.IMPORTANCE_DEFAULT);
        mNotificationManager.createNotificationChannel(channel);
    }

    mNotificationManager.notify(0, mBuilder.build());
}

希望这能帮到其他人。感谢回复。

0

当应用程序在后台运行时,如果消息包含通知,则您的服务甚至不会被调用。 Firebase 服务客户端处理通知并显示它。这就是为什么它能够正常工作。

首先,在应用程序前台调试它,并确保您从 FCM 接收到通知和通知中的数据。

如果看起来不错,可能是处理服务中的通知超过了时间限制,请尝试将通知处理移动到 onMessageReceived 中的后台线程中。这里有一个示例 http://www.zoftino.com/android-notification-data-messages-from-app-server-using-firebase-cloud-messaging


嗨,Arnav,是的,当我调试它时,我可以看到通知和数据,我不认为我需要一个后台线程来打开通知...应该更简单吧?谢谢你的回复。 - ziniestro
你的意思是因为它很简单并且不超过时间限制,所以你不需要后台线程是吗?只需尝试并查看是否有效。并告诉我服务中的故障点在哪里。当你调试它时,你看到的最后一个语句是什么? - Arnav Rao
如果您看到我的代码正在通过onMessageReceived并正确解析所有数据,最终调用sendNotification(title,message,launchPage),并且该代码片段也已执行,但我没有看到通知。 - ziniestro

0
@SuppressLint("WrongConstant")
    private void showNotification(String message) {
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
        Intent resultIntent = new Intent(getApplicationContext(), SplashActivity.class);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        resultIntent.setFlags(Notification.FLAG_AUTO_CANCEL);
        PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), 0, resultIntent, 0);
        mBuilder.setSmallIcon(R.drawable.app_icon);
        mBuilder.setContentTitle("App Name");
        mBuilder.setContentText(message);
        Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.app_icon);
        mBuilder.setLargeIcon(icon);
        mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
        mBuilder.setAutoCancel(true);
        mBuilder.setPriority(Notification.PRIORITY_HIGH);
        mBuilder.setDefaults(Notification.DEFAULT_LIGHTS);
        mBuilder.setDefaults(Notification.DEFAULT_ALL);

        if (Build.VERSION.SDK_INT> Build.VERSION_CODES.KITKAT)
        {
            mBuilder.setCategory(Notification.CATEGORY_MESSAGE);
        }
        mBuilder.setContentIntent(intent);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder.build());

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.enableVibration(true);
            notificationChannel.setShowBadge(false);
            notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            assert mNotificationManager != null;
            mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
            mNotificationManager.createNotificationChannel(notificationChannel);
        }
        assert mNotificationManager != null;

        mNotificationManager.notify(1, mBuilder.build());


    }

-1
我建议使用getData()而不是getNotification()。在getData()中,无论应用程序处于前台/后台,通知都会被接收和显示。 这里是进一步参考的文档:FCM

是的,如果我关闭应用程序,我可以看到通知出现,当我点击它时,应用程序会处理它并显示在负载中发送的页面。如果我调试应用程序并且它是打开的,我也可以看到有效载荷,但由于某种原因没有显示通知。我已经检查了设置,并启用了通知。谢谢回复。 - ziniestro
请发布上传数据的代码,这样我们就能看到到底发生了什么。 - supro_96
getData() 而不是 remoteMessage.getData()... 如果方法不存在的话... - ziniestro
好的,就像这样 -> remoteMessage.getData().get("data_Name"); 你需要传递包含数据字段的JSONArray [在FCM文档中]。它可以在后台/前台正常工作。去掉remoteMessage.getNotification()并尝试一下。数据字段是拯救我免于错误的东西。请在得到答案后更新。 - supro_96
是的...如果您检查我的代码,这就是我正在做的...remoteMessage.getData().get(Consts.LAUNCH_PAGE) - ziniestro
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接