当应用程序未运行 Firebase 通知 FCM 时目标屏幕无法打开?
问题是,当应用程序运行时,通知工作正常(通知转到目标屏幕),但是应用程序状态为 onPause 或 closwd,然后单击时通知未打开目标屏幕,我如何解决这个问题是我的代码 --- >
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static final String NOTIFICATION_CHANNEL_ID = "Sesame" ;
private static final String NOTIFICATION_NAME= "Sesame";
private static int count = 0;
private static final String TAG = MyFirebaseMessagingService.class.getName();
@Override
public void onMessageReceived(RemoteMessage message) {
super.onMessageReceived(message);
Map<String, String> data = message.getData();
String title = data.get("title");
String msg = data.get("message");
String deepLink = data.get("deepLink");
String slug = data.get("slug");
sendMyNotification(title,msg,deepLink,message);
}
private void sendMyNotification(String message, String body, String deeplink, RemoteMessage remoteMessage) {
Intent intent = new Intent(this,MainActivity.class);
if (remoteMessage.getData().size() >0){
if (deeplink.equals("deepLink")){
intent = new Intent(this, TermsConditionsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Log.i(TAG, "sendMyNotification: Terms ");
}}
intent.setPackage(null);
intent.addFlags(Intent.FLAG_FROM_BACKGROUND | Intent.FLAG_RECEIVER_NO_ABORT);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT |
PendingIntent.FLAG_ONE_SHOT |
PendingIntent.FLAG_IMMUTABLE);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,NOTIFICATION_NAME, importance);
mChannel.setDescription(message);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mNotifyManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, NOTIFICATION_NAME);
mBuilder.setContentTitle(message)
.setContentText(body)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setColor(Color.parseColor("#FFD600"))
.setContentIntent(pendingIntent)
.setChannelId(NOTIFICATION_CHANNEL_ID)
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
.setPriority(NotificationCompat.PRIORITY_HIGH);
mNotifyManager.notify(count, mBuilder.build());
count++;}}
在我的 Manifest.xml 文件中 -->
<service android:name=".notifications.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
</service>
并将目标活动设置为 ->
<activity android:name=".activities.TermsConditionsActivity" >
<intent-filter>
<action android:name="deepLink"></action>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</activity>
在我的目标活动中,我订阅了主题
FirebaseMessaging.getInstance().subscribeToTopic("topic");
请帮助我..
The problem is that When the App is running then notification is working fine (notification goes to targeted screen ) However tha app state is onPause or closwd then notification on click not open the targeted screen How i can solve it here is my code --->
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static final String NOTIFICATION_CHANNEL_ID = "Sesame" ;
private static final String NOTIFICATION_NAME= "Sesame";
private static int count = 0;
private static final String TAG = MyFirebaseMessagingService.class.getName();
@Override
public void onMessageReceived(RemoteMessage message) {
super.onMessageReceived(message);
Map<String, String> data = message.getData();
String title = data.get("title");
String msg = data.get("message");
String deepLink = data.get("deepLink");
String slug = data.get("slug");
sendMyNotification(title,msg,deepLink,message);
}
private void sendMyNotification(String message, String body, String deeplink, RemoteMessage remoteMessage) {
Intent intent = new Intent(this,MainActivity.class);
if (remoteMessage.getData().size() >0){
if (deeplink.equals("deepLink")){
intent = new Intent(this, TermsConditionsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Log.i(TAG, "sendMyNotification: Terms ");
}}
intent.setPackage(null);
intent.addFlags(Intent.FLAG_FROM_BACKGROUND | Intent.FLAG_RECEIVER_NO_ABORT);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT |
PendingIntent.FLAG_ONE_SHOT |
PendingIntent.FLAG_IMMUTABLE);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,NOTIFICATION_NAME, importance);
mChannel.setDescription(message);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mNotifyManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, NOTIFICATION_NAME);
mBuilder.setContentTitle(message)
.setContentText(body)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setColor(Color.parseColor("#FFD600"))
.setContentIntent(pendingIntent)
.setChannelId(NOTIFICATION_CHANNEL_ID)
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
.setPriority(NotificationCompat.PRIORITY_HIGH);
mNotifyManager.notify(count, mBuilder.build());
count++;}}
In my Manifest.xml file -->
<service android:name=".notifications.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
</service>
And Target Activity as ->
<activity android:name=".activities.TermsConditionsActivity" >
<intent-filter>
<action android:name="deepLink"></action>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</activity>
And in my Target Activity i subscribe the topic
FirebaseMessaging.getInstance().subscribeToTopic("topic");
Please help me..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
参考:
当应用程序在后台运行时,Firebase onMessageReceived 不会被调用
这是工作原理按照预期,只有当您的应用程序位于前台时,通知消息才会传递到您的 onMessageReceived 回调。如果您的应用程序处于后台或已关闭,则通知中心会显示一条通知消息,并且该消息中的任何数据都会传递到因用户点击通知而启动的意图。
您可以指定 click_action 来指示用户点击通知时应启动的意图。如果未指定 click_action,则使用主活动。
当意图启动时,您可以使用
getIntent().getExtras();
检索包含随通知消息一起发送的任何数据的 Set。
有关通知消息的更多信息,请参阅文档。
2:如果BG参考中的应用程序如何在onMessageReceived中调用
:Firebase onMessageReceived未调用 。 (阅读完整的答案)
从某个地方发送的消息需要有数据有效负载并且没有通知有效负载,否则它们不会一致地到达 MessagingService 中的“onMessageReceived”内部。
Reference :
Firebase onMessageReceived not called when app in background
This is working as intended, notification messages are delivered to your onMessageReceived callback only when your app is in the foreground. If your app is in the background or closed then a notification message is shown in the notification center, and any data from that message is passed to the intent that is launched as a result of the user tapping on the notification.
You can specify a click_action to indicate the intent that should be launched when the notification is tapped by the user. The main activity is used if no click_action is specified.
When the intent is launched you can use the
getIntent().getExtras();
to retrieve a Set that would include any data sent along with the notification message.
For more on notification message see docs.
2: How to get call in onMessageReceived if app in BG
Refrence : Firebase onMessageReceived not called. (read this complete answer)
Your messages, that are sent from somewhere, need to have a data payload and no notification payload, or they won't arrive consistently inside of "onMessageReceived" in your MessagingService.