Home » Android » android – Notification setFullScreenActivity

android – Notification setFullScreenActivity

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have the following code, that’s suppose to initialize a new activity during the notification, this is located in a service class

Intent push = new Intent();
push.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
push.setClass( context, MyActivity.class );
PendingIntent pi = PendingIntent.getActivity( context, 0, push, PendingIntent.FLAG_ONE_SHOT );

long[] vibraPattern = {0, 500, 250, 500 };

Notification noti = new Notification.Builder( getApplicationContext() )
     .setVibrate( vibraPattern )
     .setDefaults( Notification.DEFAULT_SOUND )
     .setFullScreenIntent( pi , true )
     .setContentIntent( pi )
     .getNotification();

notifMng.notify( 0 , noti ); 

The sound and the vibration go nicely, so the noti is successfully notified, however MyActivity is never created, even while it is the FullScreenIntent of this notification.

How to&Answers:

Notifications are tricky with the API changing so much between different versions. Your solution is targeting API level 11+ (3.0.x) and does not work on any 2.x devices. The getNotification() method is also deprecated in 4.1…

Your notification is missing content to show. It will actually launch the activity when the notification is received but does not display notification because it does not have any content to display.

Add .setFullScreenIntent( pi , true ) if you want to launch an activity immediately upon receiving the push.

I have modified your code so that it works:

Intent push = new Intent();
push.setClass(context, MyActivity.class);
PendingIntent pi = PendingIntent.getActivity(context, 0, push, 
                    PendingIntent.FLAG_ONE_SHOT);

long[] vibraPattern = { 0, 500, 250, 500 };

Notification noti = new Notification.Builder(getApplicationContext())
            .setVibrate(vibraPattern)
            .setDefaults(Notification.DEFAULT_SOUND)
            .setContentIntent(pi)
            .setContentTitle("Title")
            .setContentText("content text")
            .setSmallIcon(R.drawable.ic_launcher)
            .getNotification();

    NotificationManager notificationManager = 
        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
         notificationManager.notify(0, noti); 

Answer:

Notification.Builder requires API 11+. Use NotificationCompat.Builder instead, which is supported only requires API 4+, and allows for the implementation of new features (i.e. new notification styles available in Jelly Bean) on supported devices, while older devices just ignore the new features. It makes everything much smoother.

See this link for more details: http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html

Answer:

you need following three variables to pass:

  1. the color
  2. if turning on the led is on
  3. if turning off the led is on

if you enable 2 and 3 your led will be blinking, if you disable 2 and 3 the led will be turned off

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context );
mBuilder.setLights(Color.RED, 1, 1); // will blink