【问题标题】:I can't receive push notifications when my app is running and if it is closed i receive the notifications当我的应用程序正在运行时,我无法接收推送通知,如果它已关闭,我会收到通知
【发布时间】:2019-04-17 12:50:39
【问题描述】:

我正在使用 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】:

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

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

解释:

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

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

【讨论】:

  • 我不知道你的代码是怎样的——那就别猜了。据我们所知,他已经在发送数据负载
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多