Open application after clicking on Notification

后端 未结 11 2165
南笙
南笙 2020-11-22 13:52

I have a notification in my app with the following code:

//Notification Start

   notificationManager = (NotificationManager) getSystemService(Context.NOTIFI         


        
11条回答
  •  梦谈多话
    2020-11-22 14:49

    Thanks to above posts, here's the main lines - distilled from the longer code answers - that are necessary to connect a notification with click listener set to open some app Activity.

    private Notification getNotification(String messageText) {
    
        Notification.Builder builder = new Notification.Builder(this);
        builder.setContentText(messageText);
        // ...
    
        Intent appActivityIntent = new Intent(this, SomeAppActivity.class);
    
        PendingIntent contentAppActivityIntent =
                PendingIntent.getActivity(
                                this,  // calling from Activity
                                0,
                                appActivityIntent,
                                PendingIntent.FLAG_UPDATE_CURRENT);
    
        builder.setContentIntent(contentAppActivityIntent);
    
        return builder.build();
    }
    

提交回复
热议问题