【问题标题】:getIntent().getExtras() returns null AndroidgetIntent().getExtras() 返回 null Android
【发布时间】: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


【解决方案1】:

在制作 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);

希望对你有帮助:)

【讨论】:

    【解决方案2】:

    感谢大家,所有的答案让我更接近让它工作,但我需要将 PendingIntent 更改为以下内容。

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    

    谢谢...

    【讨论】:

      【解决方案3】:

      在创建 Pending Intent 之前移动 extras 分配

      添加标志:

      FLAG_ACTIVITY_NEW_TASK
      

      注意,当使用 PendingIntent.getActivity(...) 时,活动将在现有活动的上下文之外启动,因此您必须使用 Intent.FLAG_ACTIVITY_NEW_TASK Intent 中的启动标志。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多