我正在尝试通过Firebase云消息传递在活动中初始化fragment
。
片段调用在应用程序打开时有效,但是一旦我关闭应用程序,推送通知只会打开Activity而不是片段。
MyActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//All my initial code
Intent i = getIntent();
Bundle extras = i.getExtras();
if(extras != null) {
String push = extras.getString("push");
if (push != null) {
Integer id = Integer.parseInt(extras.getString("id"));
goToDetalleDenuncia(id);
}
}else
goToMenu();
}
我可以做些什么来调试(或解决)这个问题? 我不能使用Logcat(至少使用eclipse),因为此时应用程序已关闭
答案 0 :(得分:3)
最后,我找到了解决问题的方法。
嗯,首先,FCM通知无法直接调用片段。
您必须在意图中使用click_action
参数。
首先是 Manifest.xml
<activity
android:name=".MainActivity" android:label="@string/app_name" android:screenOrientation="portrait" android:theme="@style/AppTheme.NoActionBar">
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
<intent-filter>
<action android:name="sm.boson.com.smd.MAIN" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
在此,您必须在intent-filter
部分添加action android:name
并指定名称。在我的情况下,我使用了相同的包名称“.MAIN”(符号化MainActivity,但你可以使用其他名称)。
<强> MyFirebaseMessagingService.java 强>
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String id = remoteMessage.getData().get("tag") ;
String click_action = remoteMessage.getNotification().getClickAction();
Intent intent = new Intent(click_action);
intent.putExtra("id", id);
intent.putExtra("push", "true");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle(getResources().getString(R.string.app_name));
notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
在我的Intent
声明中,我添加了Extra
以向活动发送一些参数。
<强> MyActivity.java 强>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//ALL THE LOGIC OF YOU ONCREATE EVENT
Intent i = getIntent();
Bundle extras = i.getExtras();
if(extras != null) {
String push = extras.getString("push");
if (push != null) {
Integer id = Integer.parseInt(extras.getString("id"));
goToDetalleDenuncia(id);
}else if ( extras.getString("tag") != null ){
Integer id = Integer.parseInt(extras.getString("tag"));
goToDetalleDenuncia(id);
}else
goToMenu();
}else
goToMenu();
}
好吧,当应用程序运行时,您可以使用第一个if
语句获取参数并获取它们:extras.getString("push")
但是,如果您的应用程序在后台运行(或已关闭),您必须获得以下参数:
Integer.parseInt(extras.getString("tag"));
为什么“标记”?
在我的MyFirebaseMessagingService
中,我获得了“标记”,并将其放入Extra
中的“id”键中,用于我的意图。
我的服务器中的json就像:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"priority" : "normal",
"notification" : {
"body" : "This week's edition is now available.",
"title" : "Mytitle",
"icon" : "new"
},
"data" : {
"tag" : "3.21.15",
}
}
通过这种方式,现在我可以在应用程序运行时,在后台和关闭时使用Fragment
打开特定的FCM
。
希望这会对你有所帮助。