【发布时间】:2017-02-25 05:56:26
【问题描述】:
我想在应用收到通知以进行进一步处理时打开一个片段。在我的 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 上找到的所有解决方案,但到目前为止都没有奏效。代码一定有什么问题?
【问题讨论】:
-
简单地说,您将 Intent 发送到
SplashScreenActivity(来自MyFirebaseMessagingService)并在MainActivity中恢复它。 -
@SrikarReddy 抱歉,尝试了不同的方法,这就是原因。我实际上是把它发送到 MainActivity
-
在创建像 Pratik 提到的待处理意图之前尝试设置额外内容。
标签: java android android-intent