Search code examples
androidandroid-notificationsandroid-alarmsandroid-8.1-oreo

Notifications for an alarm clock missing functionality in Oreo


I've been building an alarm clock via youtube tutorial by Anna Xu

She ends up keeping her code within the If statement and it seems to click and bring up her application just fine. I've made modifications based on searching for fixes for Oreo on Pixel XL. This is what I have so far:

// notification parameters
        int notifyID = 1;
        String CHANNEL_ID = "my channel_01";
        CharSequence name = "Hello";
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
        // set up the notification service
        NotificationManager notify_manager = (NotificationManager)
                getSystemService(Context.NOTIFICATION_SERVICE);
        notify_manager.createNotificationChannel(mChannel);

        Notification notification_popup = new Notification.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentTitle("An alarm is going off!")
                .setContentText("Click me!")
                .setChannelId(CHANNEL_ID).build();
        // set up notification start command
        notify_manager.notify(0, notification_popup);

This brings up the notification when the alarm sounds, but clicking does not bring up the app, in fact I don't think you can click it at all, you can only swipe it away. I just want to be able to click the notification to bring the app to the foreground. I'm super new with Java and Android Studio, this was all prompted by necessity now that Oreo system alarm clock can't be trusted. Any help would be amazing.


Solution

  •  //Create pendingIntent to open your particular activity     
      Intent notificationIntent = new Intent(context, YourActivity.class);
    
                notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                        | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    
                PendingIntent intent = PendingIntent.getActivity(context, 0,
                        notificationIntent, 0);
               //Then edit here 
    
               Notification notification_popup = new Notification.Builder(this)
                        .setSmallIcon(R.drawable.ic_launcher_foreground)
                        .setContentTitle("An alarm is going off!")
                        .setContentText("Click me!")
                        .setContentIntent(notificationIntent )
                        .setChannelId(CHANNEL_ID).build();