Android setFullScreenIntent() execute the PendingIntent when notification reach and phone is locked - android

Android setFullScreenIntent() execute the PendingIntent when notification reach and phone is locked.
Strange issue facing when I using setFullScreenIntent() it will execute the PendingIntent which I have set as content when my phone is locked and when I unlocking my phone then app is opened. If phone is unlocked and notification received it'll be display notification as top on all screen and not execute the PendingIntent. Has anybody face this issue? Your suggestion will be appreciated.
Below is my code
Intent _intent = new Intent(GcmIntentService.this,NotificationActivity.class);
_intent.addCategory(Intent.CATEGORY_DEFAULT);
_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mNotificationManager =(NotificationManager)GcmIntentService.this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(GcmIntentService.this, requestCode,
_intent, PendingIntent.FLAG_UPDATE_CURRENT);
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(icon)
.setTicker(ticker)
.setContentTitle(title)
.setContentText(content)
.setAutoCancel(true)
.setWhen(System.currentTimeMillis())
.setDefaults(Notification.DEFAULT_ALL)
.setContentIntent(contentIntent);
if(CURRENT_VERSION>=LOLLIPOP_VERSION){
builder.setColor(Color.argb(255, 117,63,0));
builder.setVisibility(Notification.VISIBILITY_PUBLIC);
builder.setSmallIcon(R.drawable.app_notification_icon);
builder.setFullScreenIntent(contentIntent, false);
}
mNotificationManager.notify(NOTIFICATION_ID, builder.build());

It's not working on my Android 8.1.
If set the intent to null, it will become the normal notification without persist over heads-up area.
builder.setFullScreenIntent(null, true);

If you do not want to execute the FullScreen Intent when notification reached,just set an empty PendingIntent to FullScreen Intent.
PendingIntent fullScreenIntent = PendingIntent.getActivity(GcmIntentService.this, requestCode, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
builder.setFullScreenIntent(fullScreenIntent, true);

Remove FLAG_ACTIVITY_NEW_TASK from the intent.
Intent _intent = new Intent(GcmIntentService.this,NotificationActivity.class);
_intent.addCategory(Intent.CATEGORY_DEFAULT);

Related

NotificationCompat and setFullScreenIntent()

I was wondering if anyone has some experience with with this type of Notification.
In my use case I want to trigger a notification from my Service and than it should open a fullscreen video. The method setFullScreenIntent look just the right thing for this problem because in the documentation it writes:
An intent to launch instead of posting the notification to the status bar. Only for use with extremely high-priority notifications demanding the user's immediate attention, such as an incoming phone call or alarm clock that the user has explicitly set to a particular time.
So it says it works like an incoming phone call. That means even if my phone is asleep I should get to see the notification in full screen.
But in my case I just get the heads-up notification and if I click on it it opens the activity. Even thought in the docs they mention something about this behavior ...
On some platforms, the system UI may choose to display a heads-up notification, instead of launching this intent, while the user is using the device.
... I want to know how to automatically open the activity when the notification is triggered. Just like the incoming phone call screen.
This is my code from the service:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Intent notificationIntent = new Intent("android.intent.category.LAUNCHER");
intent.setClassName("com.example.test",
"com.example.test.VideoActivity");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(contentIntent)
.setContentTitle("Video")
.setContentText("Play video")
.setFullScreenIntent(contentIntent, true);
NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
return Service.START_STICKY;
}
try to notify notification with setFullScreenIntent at onCreate
#Override
public int onCreate() {
Intent notificationIntent = new Intent("android.intent.category.LAUNCHER");
intent.setClassName("com.example.test",
"com.example.test.VideoActivity");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(contentIntent)
.setContentTitle("Video")
.setContentText("Play video")
.setFullScreenIntent(contentIntent, true);
NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
Try removing
.setContentIntent(contentIntent)

Notification does not work properly on some devices

In my app I use a service for background work(send data to server) when my app is in the background.So I create a notification which tells to the user that app is running in the background.I want when the user taps on the notification my app to come to the foreground so I use an Intent like this that android use to launch my app
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.final_driver_notification_icon)
.setContentTitle("...")
.setContentText("...");
final Intent notintent = new Intent(getApplicationContext(),EntryScreen.class);
notintent.setAction(Intent.ACTION_MAIN);
notintent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendint = PendingIntent.getActivity(this, 0, notintent, 0);
mBuilder.setContentIntent(pendint);
int notID = 001;
startForeground(notID, mBuilder.build());
In the emulator and some physical devices works perfect.But in some other it launches the app from the start(It puts Entry screen in the top).
Any idea why is this happening?
You should start your service with startForeground(). this is the way to start a service in foreground and report by a notification. Android - implementing startForeground for a service?
or try something like this:
public void initForeground() {
Intent intentForeground = new Intent(this, Activity.class);
intentForeground.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intentForeground,0);
pendingIntent.cancel();
pendingIntent = PendingIntent.getActivity(this, 0, intentForeground,0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.icon)
.setContentIntent(pendingIntent)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.something))
.setTicker(getString(R.string.something))
.setAutoCancel(false);
Notification notification = mBuilder.build();
startForeground(myID, notification);
}
Hope it helps you!

Intent data not updated in activity while launched from andorid push notification click

I am using Google cloud messaging for my android application.GCM part is running successfully.Now I want to show the message in a UI activity when the user taps on the notification icon in the status bar.
I am launching my ResultShowActivity from the notification using the following code.
Intent notificationIntent = new Intent(context, ResultShowActivity.class);
notificationIntent.putExtra("Message",msg);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.gcm_cloud)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setAutoCancel(true)
.setSound(alarmSound)
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
The message is being sent properly,and shown correctly when the GCM notification appears but when I capture the extras of intent in ResultShowActivity it is always showing the old message.
I am using following code to receive message from intent in ResultShowActivity.
String GCMMsg = showViewIntent.getStringExtra("Message");
I have tried removing the in ResultShowActivity by using
intent.removeExtra("Message");
or
intent.replaceExtras(extras);
Can anyone please suggest how to retrieve the updated message from intent.Thanks for any help regarding this.
Please use following code
int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);
PendingIntent contentIntent = PendingIntent.getActivity(this,iUniqueId,notificationIntent, 0);
Instead of following line of code
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
Hope this will help
You need to add Intent.FLAG_ACTIVITY_SINGLE_TOP to intent flag and PendingIntent.FLAG_UPDATE_CURRENT to pending intent flag.
Intent intent = new Intent(this,YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent =
PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
Important: Make sure to implement onNewIntent(Intent intent). This function will be called when your activity is in foreground.

Android notifications when app isn't in background

I'm having this weird issue with notifications in Android. I'm creating notifications this way:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, MyClass.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.putExtra("data", value);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification updateComplete = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(msg)
.setTicker(title)
.setWhen(System.currentTimeMillis())
.setContentIntent(contentIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setSmallIcon(R.drawable.icon_notifications)
.build();
notificationManager.notify(100, updateComplete);
When the app is running (or in teh background) and hte user clicks on the notification, everything works. onNewIntent is called and the data is in the extras. But when the app is not running or in the background, then onNewIntent() is NOT called. I tried to get the intent from onCreate() using getIntent(), but it never has extras. Is there anyway to get the extras when the app is not already running?
My activities are all singleTop.
Try set some Action on intent:
notificationIntent.setAction(Long.toString(System.currentTimeMillis()));
Then get Extras in onCreate.
You may also try replace PendingIntent.FLAG_UPDATE_CURRENT with 0.

android clear intent which send from notification

My app is getting called by an intent that is passing information(pendingintent in statusbar) but when I hit the home button and reopen my app by holding the home button it calls the intent again and the same extras are still there...
is there any way to clear the intent? or check whether it has been used before?
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("reportID", reportID);
intent.putExtra("message", message);
intent.putExtra("toast_show", true);
intent.putExtra("detail_open", true);
intent.putExtra("SplashActivity", true);
PendingIntent pendingIntent = PendingIntent
.getActivity(context.getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
// Intent.FLAG_ACTIVITY_NEW_TASK
/* get the system service that manage notification NotificationManager */
NotificationManager notificationManager = (NotificationManager) context
.getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
context.getApplicationContext())
.setWhen(when)
.setContentText(message)
.setContentTitle(title)
.setSmallIcon(smalIcon)
.setAutoCancel(true)
.setTicker(message)
.setLargeIcon(largeIcon)
.setDefaults(
Notification.DEFAULT_LIGHTS
| Notification.DEFAULT_VIBRATE
| Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent);
/* Create notification with builder */
Notification notification = notificationBuilder.build();
notificationManager.notify(0, notification);
The problem starts when I receive a notification. If I start the app from the notification, it starts the correct screen and I can use the app => ok. But when I quit the app (with home or back button) it do not appears anymore in the "recent app" list. More precisely:
Does anyone knows how to keep the app in the "recent app" list after being started form a notification?
Update this line and try
PendingIntent pendingIntent = PendingIntent.getActivity(context, (int)(Math.random() * 100),intent,PendingIntent.FLAG_UPDATE_CURRENT);
What you need is the PendingIntent.FLAG_CANCEL_CURRENT :
If the described PendingIntent already exists, the current one is canceled before generating a new one. You can use this to retrieve a new PendingIntent when you are only changing the extra data in the Intent; by canceling the previous pending intent, this ensures that only entities given the new data will be able to launch it.
PendingIntent pendingIntent = PendingIntent.getActivity(
context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
This will ensure that your latest data will pe present on your Intent.

Categories

Resources