Android-Firebase Notification在应用程序在后台运行时无法打开目标活动,而在前台运行正常 [英] Android - Firebase Notification not opening targeted activity when app is in background but working properly in foreground
问题描述
我已经尝试了stackoverflow的一些解决方案.但在我的情况下无法正常工作.
下面是我的MyFirebaseMessagingService
类,它扩展了FirebaseMessagingService
I have tried some solution from the stackoverflow . But not working properly in my case.
Below is my MyFirebaseMessagingService
class which extends FirebaseMessagingService
public class MyFirebaseMessagingService extends FirebaseMessagingService {
String url , title , body ;
Map<String,String> data = new HashMap<>();
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
title = remoteMessage.getNotification().getTitle();
body = remoteMessage.getNotification().getBody();
if(remoteMessage.getData().size()>0){
Log.d("DATA", remoteMessage.getData().toString());
try{
data = remoteMessage.getData();
url = data.get("url");
}
catch (Exception e){
Log.e("Error", e.toString());
}
showNotification(url , title , body);
}
}
private void showNotification(String url, String title, String body) {
Intent intent = new Intent(this, NotificationViewer.class);
intent.putExtra("URL",url);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) ;
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(body);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSmallIcon(R.drawable.logo);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());
}
}
下面是我的AndroidManifest.xml
代码.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dcastalia.com.munshijobsportal">
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:name=".Controller.AppController"
android:allowBackup="true"
android:icon="@drawable/logo"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".activity.SplashActivity"
android:noHistory="true"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".firebase.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service android:name=".firebase.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<activity android:name=".activity.NotificationViewer"
android:exported="true">
</activity>
</application>
</manifest>
我已经尝试了一些解决方案,但不适用于我的情况.当应用程序为前台但不能在后台运行时,一切工作正常. 我想在用户单击通知时打开特定的活动名称NotificationViewer.Java
活动.但是当我的应用程序在后台运行时,它将继续打开我的启动器活动.
I have tried out some solution but not working in my case. Everything is working fine when app is foreground but not working in background. I want to open a particular activity name NotificationViewer.Java
activity when user click on the notification. But when my app is in background it keep opening my Launcher Activity.
推荐答案
您需要在通知请求中的json负载中设置点击动作 'click_action'=>'YourTAG'并在清单活动中应用相同的标签
You need to set click action in your json payload in notification request 'click_action' => 'YourTAG' and apply same tag in your manifest activity
<activity
android:name=".activity.NotificationViewer">
<intent-filter>
<action android:name="YourTAG" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
这篇关于Android-Firebase Notification在应用程序在后台运行时无法打开目标活动,而在前台运行正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!