我已经实现了有效负载中包含的clickAction
Firebase,它将由Android清单的Intent过滤器处理。
在应用程序的前景或后台状态单击通知后,将执行.FcmNotificationTray
活动。但是当应用程序被终止或强制关闭时,假设我们仍然在系统托盘上保留通知,则一旦点击该活动将被执行,但是第二次用户选择并单击它将不会执行的通知。
总而言之,当用户第二次选择并打开通知时(状态是应用程序被杀),它将不会执行活动,更可能是clickAction
无法控制意图过滤器。
有效载荷:
{
"notification": {
"alert": "sample Alert | Green",
"data": "Timestamp:\n 2016/09/04 13:16:44,
"sound": "default",
"gcmNotification": {
"title": "Sample App,
"body": "Sample Body | Red",
"timeToLive": 10,
"clickAction": "FIREBASE_ACTIVITY"
}
},
"users": [
"demo@firebase.com"
]
}
意图过滤器:
<activity
android:name=".activities.FcmNotificationTray"
android:label="TITLE"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="FIREBASE_ACTIVITY"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
编辑1: 这是我在用户点击通知后将执行的活动的代码。
public class FcmNotificationTray extends AppCompatActivity {
AppPreferences mPreferences;
StringProcessor mStringsplit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onShowPushNotifDialog();
}
@Override
public void onResume(){
super.onResume();
onShowPushNotifDialog();
}
private void onShowPushNotifDialog(){
/* Instantiate Preferences */
mPreferences = new AppPreferences(this);
/* Get data from notification tray center for the push notification */
Bundle bundle = getIntent().getExtras();
/* Call method to split string and set data for shared preference */
mStringsplit = new StringProcessor();
mStringsplit.onCreateSplitString(bundle.getString("data"),bundle.getString("alert"),this);
/* This will be executed if the app is killed or no current activity */
if (isTaskRoot()) {
Intent intent = new Intent(this,SplashScreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}else{ finish();}
}
}
编辑2 :FCM接收器的代码
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
/* Call Function to show notification */
showPushNotification(remoteMessage.getData().get("data"),
remoteMessage.getData().get("alert"));
}
}
/* For Push Notification */
public void showPushNotification(String data,String alert){
/* Call method to split string and set data for shared preference */
mStringsplit = new StringProcessor();
mStringsplit.onCreateSplitString(data,alert,getApplicationContext());
/* Get shared Preferences for app in background or foreground */
mPreference = new AppPreferences(getApplicationContext());
Boolean actVisibility = mPreference.getVisiblityActivity();
if(actVisibility) {
/* If application is in foreground execute the dialog
* The purpose of executing an acitivity is to get the context
*/
Intent intentdemo = new Intent().setClassName(getPackageName(),getPackageName()
+ mPreference.getClassName());
intentdemo.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentdemo.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
intentdemo.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intentdemo);
}