我想在应用收到进一步处理的通知时打开一个片段。在我的MainActivity.java中,getIntent().getExtras()
始终返回null
。
MyFirebaseMessagingService.java
private void sendNotification(String title, String messageBody, String newsID, String newsType) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Bundle extras = new Bundle();
extras.putString("newsID", String.valueOf(newsID));
extras.putString("newsType", String.valueOf(newsType));
intent.putExtras(extras);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
MainActivity.java
Bundle i = getIntent().getExtras();
if (i != null) {
for (String key : getIntent().getExtras().keySet()) {
String value = getIntent().getExtras().getString(key);
Log.e("News ID", value);
if (key.equals("newsID") ) {
dbHelper.insertNewsID(String.valueOf(value));
//show news details
getSupportFragmentManager()
.beginTransaction()
.replace(ng.naijaleague.R.id.frame_container, new NewsDetails())
.addToBackStack(null).commit();
}
}
String newsType = getIntent().getExtras().getString("newsType");
if ("newsType".equals(newsType) ) {
String value = getIntent().getExtras().getString("newsType");
if("Local".equals(value)){
//add news type to shared preference
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("newType", "Local");
editor.apply();
}else{
//add news type to shared preference
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("newType", "Foreign");
editor.apply();
}
}
}
我已经完成了我在SO上找到的所有解决方案,但到目前为止它们都没有。代码有什么问题?
答案 0 :(得分:2)
在发出 PendingIntent 对象:P
之前发布这4行Bundle extras = new Bundle();
extras.putString("newsID", String.valueOf(newsID));
extras.putString("newsType", String.valueOf(newsType));
intent.putExtras(extras);
LIKE:
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//PUT HERE
Bundle extras = new Bundle();
extras.putString("newsID", String.valueOf(newsID));
extras.putString("newsType", String.valueOf(newsType));
intent.putExtras(extras);
//THEN Create PendingIntent Object
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
希望它能帮到你:)。
答案 1 :(得分:1)
在创建待定意图之前移动额外内容
添加标志:
FLAG_ACTIVITY_NEW_TASK
注意,与使用 PendingIntent.getActivity(...)时相比,活动将在现有活动的上下文之外启动,因此您必须使用 Intent.FLAG_ACTIVITY_NEW_TASK 在意图中启动标志。
答案 2 :(得分:1)
感谢所有人,所有答案都让我更接近于使其成功但我需要将PendingIntent更改为以下内容以确定它。
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
感谢...< 3