当我在firebasemessagingservice(fcm)
中添加额外的意图时,当我单击通知后,它打开了activity(welcomeactivty),但是oncreate()
方法我获得了额外的捆绑包,但没有得到相应的额外价值应用程序在后台运行时的关键,相反,当应用程序在前台运行时,我得到了值。
我也尝试过onnewIntent()
来获取价值。我还尝试了在堆栈溢出上阅读的解决方案。
这是我的lucher活动方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_creation);
// Log.d("aaabbb", savedInstanceState.getInt("abc")+"save");
Bundle extras = getIntent().getExtras();
Log.d("aaabbb", "Start");
if (extras != null) {
// this always true getIntent().getIntExtra("abc",200)+"a" return null
Log.d("aaabbb", getIntent().getIntExtra("abc",200)+"a");
if (extras.containsKey("abc")) {
// extract the extra-data in the Notification
String msg = extras.getString("abc");
Log.d("aaabbb", msg);
}
}
}
这是我的FCM颂歌:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String title=remoteMessage.getNotification().getTitle();
String body=remoteMessage.getNotification().getBody();
String from_userid=remoteMessage.getData().get("chatid");
// String
super.onMessageReceived(remoteMessage);
//SharedPreferencesSuk.getInstance(this).put(SharedPreferencesSuk.Key.STATUS,+"");
m=System.currentTimeMillis()+"";
Intent intent;
if(title.equals("Message"))
{
intent = new Intent(this,MessagingView.class);
intent.putExtra("abc", -25);
intent.putExtra("otheruserid",from_userid);
}
else
{
intent = new Intent(this,HomeActivity.class);
intent.putExtra("abc", -25);
intent.putExtra("otheruserid",from_userid);
}
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
String channelId = "Default";
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody()).setAutoCancel(true).setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
manager.notify(0, builder.build());
//-------
}