2015-09-27 3 views
0

Мне нужно создать новый объект, когда пользователь приходит от push notification.Как определить, какой вид onResume произошел

Когда пользователь нажимает на уведомление, onResume запускается (по назначению). В методе onResume мне нужно сделать вызов на сервер и создать объект на основе ответа. Однако я не могу понять, как определить, был ли вызван onResume уведомлением или чем-то другим, например, включением экрана.

Я не очень разбираюсь в Intent flags, но, возможно, решение может лежать там.

Это код, который настраивает и запускает push notification:

private void sendNotification(String message, String topic) { 
    Intent intent = new Intent(this, MainActivity.class); 
    intent.putExtra("resumeType", "BattleNotification"); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
      PendingIntent.FLAG_ONE_SHOT); 

    String contentTitle = "Message from Maguss"; 
    switch (topic){ 
     case "battle": 
      contentTitle = "Your battle was accepted"; 
      break; 
    } 
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.icon) 
      .setContentTitle(contentTitle) 
      .setContentText(message) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

    NotificationManager notificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
} 

ответ

0

В вашей деятельности вы можете получить намерение, который начал его, а затем проверить его, чтобы узнать, было ли это от уведомления или нет. Возможно, что-то вдоль линий

if (getIntent().getStringExtra("resumeType") != null) { /* from notification */ }

будет работать для вас.

0

В вашем методе onResume вы можете проверить, пришли ли вы из push-уведомления или из другого мероприятия. См. Следующий код

@Override 
protected void onResume() { 
    super.onResume(); 
    String resumeType = getIntent().getStringExtra("resumeType"); 
    if (resumeType!=null && resumeType.equalsIgnoreCase("BattleNotification")){ 
      // from push notification 
    }else{ 

    } 
} 
Смежные вопросы