1

这里是我的情况下通知: 通知时到达,应用程序可能是前景,背景,或关闭Android的推送通知

1.对于前景: 当用户通知自来水,应用程序应该显示主要活动。

2.对于背景: 当通知用户抽头,应用程序应该引起前和应显示主要业务

3.当应用程序是封闭: 当通知用户抽头,应用应打开,并应显示主要活动。

当我设置意图与Intent.FLAG_ACTIVITY_SINGLE_TOP & PendingIntent与PendingIntent.FLAG_ONE_SHOT。它按预期工作,但是当用户点击通知时,它不会调用主活动的onCreate方法。

private void showNotification() { 

     Intent intent = new Intent(this, DashboardActivity.class); 
     intent.putExtra(AppConstants.SCREEN, screen); 
     intent.putExtra(AppConstants.USER_ID, user_id); 
     intent.putExtra(AppConstants.TABLE_ID, table_id); 
     intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 

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

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(getNotificationIcon()) 
      .setContentTitle(getResources().getString(R.string.app_name)) 
      .setContentText(message) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

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

    notificationManager.notify(0, notificationBuilder.build()); 
    } 

如果你认为上面的代码则需要从MainActivity(DashboardActivity)的通知时,使用者开关执行的方法。

注意:当应用程序是前台/后台时,它不应该关闭并打开新的。它必须在现有应用程序中更新。

希望你了解我的情况。提前致谢。

回答

0

当用户点击一个意图使用"Intent.FLAG_ACTIVITY_SINGLE_TOP"的通知时,将不会调用意图的onCreate方法。它会调用一个方法,在通知时用户点击,这是写在下面:

@Override 
protected void onNewIntent(Intent intent) {} 

感谢您阅读了这个问题:)