【问题标题】:Launch Activity when notification is clicked单击通知时启动 Activity
【发布时间】:2016-05-30 10:24:12
【问题描述】:

当我单击状态栏中的通知时,我想打开一个活动。我在 StackOverflow 上看到了这个答案,但这些答案都不适合我。这是我的代码:

    notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder.setProgress(100, 0, false);
    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setSmallIcon(R.drawable.ic_action_file_cloud_upload);
    notificationBuilder.setContentTitle(getString(R.string.notification_upload_title));

    //when this notification is clicked and the upload is running, open the upload fragment
    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setAction(Intent.ACTION_MAIN);
    notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);

    // set intent so it does not start a new activity
    PendingIntent intent = PendingIntent.getActivity(this, 1, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
    notificationBuilder.setContentIntent(intent);

    this.startForeground(NOTIFICATION_ID_UPLOADING_PROGRESS, notificationBuilder.build());

Manifest.xml

<activity
        android:name="com.android.app.MainActivity_"
        android:label="@string/app_short_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

【问题讨论】:

  • 你为什么用this.startForeground而不是NotificationManager.notify
  • 点击通知会发生什么?
  • @Malith Lakshan,状态栏已关闭,没有任何反应。
  • 检查提供给待处理意图的上下文是否正确,尝试使用 ClassName.this,如果它来自服务或活动

标签: android android-notifications


【解决方案1】:

我终于意识到我必须这样写:

Intent notificationIntent = new Intent(this, MainActivity_.class);

而不是

Intent notificationIntent = new Intent(this, MainActivity.class);

非常感谢您的所有回答!

【讨论】:

    【解决方案2】:

    我认为您不需要所有这些标志和配置来从 PendingIntent 打开一个活动。你当然不需要

    notificationIntent.setAction(Intent.ACTION_MAIN);
    notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    PendingIntent.FLAG_ONE_SHOT
    

    删除它们并重试。

    此外:MainActivity 的包名是否符合预期(com.android.app.MainActivity)?

    【讨论】:

      【解决方案3】:

      试试下面的解决方案希望它对你有用

      Intent intent = new Intent(this,YourActivity....class);
              intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
              PendingIntent blogIntent = PendingIntent.getActivity(this, INT CONSTANTS, intent,
                      PendingIntent.FLAG_ONE_SHOT);
      

      来自通知

      NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
      .
      .
      .
      notificationBuilder.setContentIntent(blogIntent);
      
      
      Notification notification = notificationBuilder.build();
      NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
      notificationManager.notify(Constants.NOTIFICATION_NEW_BLOG, notification);
      

      【讨论】:

        【解决方案4】:

        您必须在清单中添加自定义接收器

               <receiver
                    android:name=".IntentReceiver"
                    android:exported="false">
                    <intent-filter>
        
                        <!-- add notification action here  -->
        
                    </intent-filter>
                </receiver>
        

        在 Intent 接收器类中重写接收方法

        public class IntentReceiver extends BroadcastReceiver{
        @Override
            public void onReceive(Context context, Intent intent) {
                //call your activity here
            }
        } 
        

        【讨论】:

        • 我已经尝试过了,但没有成功。这个 onReceived 方法没有被调用
        【解决方案5】:
        Use this code
        
        /* Invoking the default notification service */
                NotificationCompat.Builder  mBuilder = new NotificationCompat.Builder(this);
        
                mBuilder.setContentTitle("Test");
                mBuilder.setContentText(text2);
                mBuilder.setSmallIcon(R.drawable.icon);
        
           /* Creates an explicit intent for an Activity in your app */
                Intent resultIntent = new Intent(this, NotificationListActivity.class);
        
                TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
                stackBuilder.addParentStack(NotificationListActivity.class);
        
           /* Adds the Intent that starts the Activity to the top of the stack */
                stackBuilder.addNextIntent(resultIntent);
                PendingIntent resultPendingIntent =stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
        
                mBuilder.setContentIntent(resultPendingIntent);
                NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        
           /* notificationID allows you to update the notification later on. */
                mNotificationManager.notify(9999, mBuilder.build());
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-04-06
          • 2017-05-31
          • 2012-08-02
          • 2012-04-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多