【问题标题】:how i can intent from notification?我怎么能从通知中获得意图?
【发布时间】:2016-11-17 14:29:18
【问题描述】:

为什么通知中的Intent 为空?

Intent resultIntent = new Intent(context, myclass.class);
        resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        Bundle extras=new Bundle();
        extras.putString("key","value");
        PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        context,
                        0,
                        resultIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
            NotificationCompat.Builder n = new NotificationCompat.Builder(context)
                    .setContentTitle("test")
                    .setContentText(text)
                    .setSound(sound)
                    .setContentIntent(resultPendingIntent)
                    .setAutoCancel(true)
                    .setExtras(extras)
                    .setSmallIcon(android.R.drawable.ic_media_play);
            NotificationManager notificationManager = (NotificationManager) context.getSystemService((context.getApplicationContext().NOTIFICATION_SERVICE));
            notificationManager.notify(0, n.build());
    }

但是在onStart() myclass 中,当我调用getintent().getExtras() 时,结果是null,为什么? 我如何从通知 Intent 中getExtras()

【问题讨论】:

    标签: android android-intent notifications


    【解决方案1】:

    我想你忘记打电话了。

    resultIntent.putExtras(extras);
    

    在调用它之前。

    【讨论】:

    • 好的,但是 NotificationCompat.Builder 中的 .setExtras(extras) 是什么以及如何获取它?
    • 实际上我从来没有使用过那个..但是我就像我说的那样在通知中发送了数据..它有效吗?
    【解决方案2】:

    这对我有用,你可以从中得到灵感:

     Intent notificationClickIntent = new Intent(context, NotificationAppListing.class);
                notificationClickIntent.putExtra("notificationType", notificationTypeOne);
                notificationClickIntent.putExtra("notificationUrl", notificationUrl);
    
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
            stackBuilder.addParentStack(Search.class);
            stackBuilder.addNextIntent(notificationClickIntent);
            PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
            notificationBuilder.setContentIntent(pendingIntent);
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(mId, notificationBuilder.build());
    

    【讨论】:

      【解决方案3】:

      您应该将您的附加信息添加到您的 resultIntent,而不是 NotificationCompat.Builder(而不是 resultPendingIntent

      Bundle extras=new Bundle();
      extras.putString("key","value");
      resultIntent.putExtras(bundle);
      

      或者简单地说:

      resultIntent.putExtra("key","value");
      

      【讨论】:

        【解决方案4】:

        你需要放置

        resultIntent.putExtras(bundle)
        

        在捆绑包被分配键值对之后
        然后你可以得到值

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
        String yourValue = extras.getString("key");    
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-12-12
          • 2020-12-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-10-15
          相关资源
          最近更新 更多