当应用程序收到进一步处理的通知时,我想打开一个片段。在我的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();
            }
        }
    }我已经完成了我找到的所有解决方案,但到目前为止都没有奏效。代码一定有什么问题?
发布于 2017-02-25 07:58:46
感谢每个人,所有的答案使我更接近使它工作,但我需要将PendingIntent修改为下面的方法来确定它。
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);Thanks...<3
发布于 2017-02-25 06:13:29
在创建PendingIntent对象之前,张贴这4行:P
Bundle extras = new Bundle();
extras.putString("newsID", String.valueOf(newsID));
extras.putString("newsType", String.valueOf(newsType));
intent.putExtras(extras);比如:
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);希望它能帮助你:)
发布于 2017-02-25 06:15:58
在创建暂挂意图之前,移动附加分配,
添加旗子:
FLAG_ACTIVITY_NEW_TASK注意,与使用PendingIntent.getActivity(...)时相比,该活动将在现有活动的上下文之外启动,因此您必须在意图中使用Intent.FLAG_ACTIVITY_NEW_TASK启动标志。
https://stackoverflow.com/questions/42452513
复制相似问题