How to automatically open app when receive push notification?

后端 未结 3 623
长发绾君心
长发绾君心 2021-01-13 19:22

I want to automatically open app when receive push notification. I\'ve tried but it still does not work as I expected. This code below is work when the app is active or in M

3条回答
  •  伪装坚强ぢ
    2021-01-13 20:03

    int requestID = (int) System.currentTimeMillis();
    Intent notificationIntent = new Intent(getApplicationContext(), NotificationActivity.class);
    
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    
    PendingIntent contentIntent = PendingIntent.getActivity(this, requestID,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    

    And add PendingIntent like this

    notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle("Notification")
                .setSmallIcon(R.mipmap.icon_notif)
                .setContentText(messageBody)
                .setContentIntent(contentIntent);
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setDefaults(Notification.DEFAULT_LIGHTS );
    

提交回复
热议问题