首页 > 解决方案 > 当我的应用程序运行时,我无法收到推送通知,如果它已关闭,我会收到通知

问题描述

我正在使用 firebase 处理推送通知。当我的应用程序关闭或在后台时,我可以收到通知,但是当我的应用程序运行时我没有收到通知请帮助我,这样即使我的应用程序正在运行,我也可以收到通知

  @Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    shownotifeaction(remoteMessage.getNotification().getBody());
}

public void shownotifeaction(String message) {
    PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, Splash_Img.class), 0);
    Notification notification = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_name)
            .setContentTitle("Porwal")
            .setContentText(message)
            .setContentIntent(pi)
            .setAutoCancel(true)
            .build();


    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(0, notification);
}

}

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

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

标签: androidfirebasepush-notification

解决方案


我建议您从您的服务器尝试这种形式的 json:

{ 
"data": 
{ 
"title":"FCM Message", "Body":"This is an FCM Message" }, 
"to" : "devicetoken"
} 

解释:

通知 - 仅当您的应用程序处于后台/被杀死时直接进入 Android 通知托盘的消息,或者如果您的应用程序处于前台,则传递到 onMessageReceived() 方法。

数据负载 - 无论您的应用程序是在前台还是后台还是被杀死,这些消息都将始终传递给 onMessageReceived() 方法。


推荐阅读