Send a notification when the app is closed

后端 未结 3 2029

How is it possible to send a notification programmatically, when the App got completely closed?

Example: The User closed the App, also in the Android Taskmanager, a

3条回答
  •  渐次进展
    2020-12-01 03:55

    You can use alarm manager to do this. Follow below steps :

    1) Use alarmmanager to create an alarm of after X seconds.

    Intent intent = new Intent(this, AlarmReceiver.class);
    intent.putExtra("NotificationText", "some text");
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, ledgerId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, 'X seconds in milliseconds', pendingIntent);
    

    2) Use a AlarmBroadCast receiver in your app.

    Declare in manifest file :

    
        
            
    
            
        
    
    

    3) In the broadcast receiver's on receive, you can create the notification.

    public class AlarmReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            // create notification here
        }
    }
    

提交回复
热议问题