我的应用程序运行时无法接收推送通知,如果关闭则会收到通知

问题描述 投票:-4回答:1

我正在使用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" />
android firebase push-notification
1个回答
0
投票

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

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

说明:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.