【问题标题】:Clear Pending Intent清除待定意图
【发布时间】:2019-09-25 05:44:41
【问题描述】:

通过点击通知打开应用,然后清除意图操作

如果app在后台,推送本地通知,点击通知打开MainActivity

public static void publishLocalNotification(...){
  NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, "Start")
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setSmallIcon(R.drawable.local_notification)
                .setContentTitle(title)
                .setPriority(Notification.PRIORITY_HIGH) 
                .setContentText(message);

  resultIntent = new Intent(mContext, MainActivity.class);

  resultIntent.setAction(action);
  ...
  PendingIntent pendingIntent = PendingIntent.getActivity(mContext,-1, resultIntent, PendingIntent.FLAG_ONE_SHOT);

  builder.setContentIntent(pendingIntent);

}


In the Main activity 

@Override
protected void onResume() {
  super.onResume();
  String action = getIntent().getAction();

  if(action != null){

     ... Show dialog



  }

}





如果应用无法清除意图操作。每次,当应用程序从后台转到前台时,都会调用 onResume() 并且每次都会显示对话框

【问题讨论】:

标签: android android-notifications android-pendingintent


【解决方案1】:

在此处查看我对这个问题的广泛讨论(“通知”中Intent 中的持久数据):

Remove data from notification intent

如果您只想确保对话框显示一次,您可以在onResume() 中执行以下操作:

Intent intent = getIntent(); // Get the Intent that the app was started with

if (intent.getAction() ) {
    // Show dialog ...
}
intent.setAction(null); // Clear the ACTION in the intent and
setIntent(intent);      //  replace it to prevent the dialog from being shown again

但我可能没有 100% 理解您的要求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多