首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >getIntent().getExtras()返回空Android

getIntent().getExtras()返回空Android
EN

Stack Overflow用户
提问于 2017-02-25 05:56:26
回答 3查看 4.9K关注 0票数 2

当应用程序收到进一步处理的通知时,我想打开一个片段。在我的MainActivity.java中,getIntent().getExtras()总是返回null

MyFirebaseMessagingService.java

代码语言:javascript
运行
复制
  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

代码语言:javascript
运行
复制
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();
            }
        }
    }

我已经完成了我找到的所有解决方案,但到目前为止都没有奏效。代码一定有什么问题?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-02-25 07:58:46

感谢每个人,所有的答案使我更接近使它工作,但我需要将PendingIntent修改为下面的方法来确定它。

代码语言:javascript
运行
复制
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

Thanks...<3

票数 2
EN

Stack Overflow用户

发布于 2017-02-25 06:13:29

在创建PendingIntent对象之前,张贴这4行:P

代码语言:javascript
运行
复制
Bundle extras = new Bundle();
extras.putString("newsID", String.valueOf(newsID));
extras.putString("newsType", String.valueOf(newsType));
intent.putExtras(extras);

比如:

代码语言:javascript
运行
复制
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);

希望它能帮助你:)

票数 3
EN

Stack Overflow用户

发布于 2017-02-25 06:15:58

在创建暂挂意图之前,移动附加分配,

添加旗子:

代码语言:javascript
运行
复制
FLAG_ACTIVITY_NEW_TASK

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42452513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档