Search code examples
androidandroid-notifications

Local notification when tapped open the android app


I use this code to create a local notification and show it after 30 seconds using AlarmManager.

  NotificationCompat.Builder builder = new NotificationCompat.Builder( context, default_notification_channel_id ) ;
    String currentDate = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).format(new Date());
    builder.setContentTitle( "Check new words for "+currentDate ) ;
    builder.setContentText("30 seconds delay") ;
    builder.setDefaults(Notification.DEFAULT_SOUND);
    builder.setSmallIcon(R.mipmap.ic_launcher_round ) ;
    builder.setAutoCancel( true ) ;
    builder.setChannelId( NOTIFICATION_CHANNEL_ID ) ;

    Intent notificationIntent = new Intent( context, AlarmReceiver. class ) ;
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    notificationIntent.putExtra(AlarmReceiver. NOTIFICATION_ID , 1 ) ;
    notificationIntent.putExtra(AlarmReceiver. NOTIFICATION , builder.build()) ;
    PendingIntent pendingIntent1 = PendingIntent. getBroadcast ( context, 6977 , notificationIntent , PendingIntent. FLAG_UPDATE_CURRENT ) ;
    long futureInMillis = SystemClock. elapsedRealtime () + 30000 ;

    assert alarmManager != null;


    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP , futureInMillis , 1000*10,pendingIntent1);

This is the code from the AlarmReceiver

import static com.example.couponnectcom.MainActivity.NOTIFICATION_CHANNEL_ID;
public class AlarmReceiver extends BroadcastReceiver {
    public static String NOTIFICATION_ID = "hello" ;
    public static String NOTIFICATION = "aha" ;
    @Override
    public void onReceive(Context context, Intent intent) {

        // For our recurring task, we'll just display a message
    
  
        ActivityManager.RunningAppProcessInfo myProcess = new ActivityManager.RunningAppProcessInfo();
        ActivityManager.getMyMemoryState(myProcess);
        Boolean isInBackground;
        isInBackground = myProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
        if(isInBackground) {
            Toast.makeText(context.getApplicationContext(), "12. APPLICATION BACKGROUND RUN NOTIFICATION", Toast.LENGTH_LONG).show();
            NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context. NOTIFICATION_SERVICE ) ;
            Notification notification = intent.getParcelableExtra( NOTIFICATION ) ;
            if (android.os.Build.VERSION. SDK_INT >= android.os.Build.VERSION_CODES. O ) {
                int importance = NotificationManager. IMPORTANCE_HIGH ;
                NotificationChannel notificationChannel = new NotificationChannel( NOTIFICATION_CHANNEL_ID , "NOTIFICATION_CHANNEL_NAME" , importance) ;
                assert notificationManager != null;
                notificationManager.createNotificationChannel(notificationChannel) ;
            }
            int id = intent.getIntExtra( NOTIFICATION_ID , 0 ) ;
            assert notificationManager != null;
            notificationManager.notify(id , notification) ;
        }
        

    }
}

When the user taps on the notification i want my Android app to open...how is this possible?


Solution

  • You need to use a PendingIntent. See the documentation for more information.

    For explicit instructions on how to start an Activity from a Notification with a PendingIntent see this article.

    PendingIntent openActivityIntent = 
        PendingIntent.getActivity(context, 
                                  0, 
                                  new Intent(context, YourActivity.class), 
                                  0);
    builder.setContentIntent(openActivityIntent);