android - 当应用程序打开时,应用程序未显示接收 FCM 的通知

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

当我从 Firebase 发送推送时,如果应用程序在后台或已关闭,我会收到通知,但当应用程序打开时......

调试 我观察到它在 MyMessagingService 中特别是在 onMessageReceived 处停止,所以我想我的问题是关于生成通知(或者通知的 Intent )。

我已经实现了服务:

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());
} }

在我的 list 中我有:

 <!-- [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>

关于当应用程序处于前台时如何生成通知的任何建议?

最佳答案

更改您的 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());
     }
 }

检查 - How to handle the fire base notification when app is in foreground

关于android - 当应用程序打开时,应用程序未显示接收 FCM 的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48918490/

相关文章:

java - Android 上复杂数学过程中的 Stackoverflow

Android - Proguard 重复 zip 条目错误 - Android 支持库

java - Rxjava中如何在一个线程中处理Observable的所有事件,但在另一个线程中获取可用的最新值?

javascript - 如何在vuetify项目中更新firebase文档数据onclick

android - 带有firebase crashlytics的hermes bundle的react-native源图

firebase - Firestore/Flutter - 如何获取文档 ID?

android - 如何让 SNS 在 Android 中工作?

ios - 更改推送通知声音

Android 小部件大小不正确

django - 在 Django 中的特定时间触发通知