待定意图似乎没有通过额外的

时间:2017-07-31 09:36:24

标签: android android-intent android-pendingintent

我使用的是当用户点击推送通知时启动的Intent。我有一些问题通过挂起的意图传递额外的东西。当我这样做时:

Intent i = new Intent(this, DashboardActivity.class);
i.putExtra("pending", true);
i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);

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

我期待这种行为:

  1. 待处理的意图启动DashboardActivity
  2. 在DashboardActivity内部,我会检查其他内容
  3. 如果我有额外内容,请转到活动B
  4. 相反,会发生什么:

    1. 待处理的意图启动DashboardActivity
    2. Extras为null
    3. 这是检查DashboardActivity的附加功能的代码:

      if (getIntent().getExtras() != null) {
          Intent i = new Intent(DashboardActivity.this, B.class);
          startActivity(i);
      }
      

      我没有意识到如何戒掉这种情况,所以如果有人能帮助我,我会感激不尽!

      编辑:

      private void sendNotification(String messageBody){
          String id="",message="",title="";
      
          if(type.equals("json")) {
              try {
                  JSONObject jsonObject = new JSONObject(messageBody);
                  id = jsonObject.getString("id");
                  message = jsonObject.getString("message");
                  title = jsonObject.getString("title");
      
              } catch (JSONException e) {
                  //            }
              }
          }
          else if(type.equals("message"))
          {
              message = messageBody;
          }
      
          Intent i = new Intent(this, DashboardActivity.class);
          i.putExtra("pending", true);
          i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
          i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
      
          PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);
      
          NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this);
          notificationBuilder.setContentTitle(getString(R.string.app_name));
          notificationBuilder.setContentText(message);
          Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
          notificationBuilder.setSound(soundUri);
          notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
          notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(),R.mipmap.ic_launcher));
          notificationBuilder.setAutoCancel(true);
          Vibrator v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
          v.vibrate(1000);
          notificationBuilder.setContentIntent(pendingIntent);
      
          NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
          notificationManager.notify(0,notificationBuilder.build());
      }
      

1 个答案:

答案 0 :(得分:1)

尝试使用PendingIntent.FLAG_UPDATE_CURRENT

同时添加Intent.FLAG_ACTIVITY_SINGLE_TOP,这样如果您的活动已经打开,则不会重新创建。

将上述2个标志添加到您的意图中。

同样在您的DashboardActivity实现onNewIntent

@Override
public void onNewIntent(Intent intent){
    Bundle extras = intent.getExtras();
    if(extras != null){
        if(extras.containsKey("pending"))
     {

     }
  }