Notifications don't update unless Breakpoint - android

I have a service that calls a BroadcastReceiver. It was working fine but after I changed the back-end system for handling notification ids, the final notification update which should update a notification to tell the user that a process has finished. However, that final update would not pass through unless I place a breakpoint in debugging mode in the method handling those notifications. If a breakpoint is not placed or debug mode is not active, that final notification update would not happen and the notification would display its last state which tells the user that the process has not finished, which is not the case.
Here's the method, and the Log.d()s show that the manager.notify() method was called properly to update the notification to signify that the process has finished.
#Override
public void onReceive(Context context, Intent intent) {
Download data = null;
try {
data = (Download) intent.getSerializableExtra(Commons.ARGS.DATA);
} catch (ClassCastException | NullPointerException e) {
e.printStackTrace();
}
switch (intent.getIntExtra(Commons.ARGS.RESULT, Commons.ARGS.FAILED)) {
case Commons.ARGS.DESTROY:
Log.w(TAG, "Service was destroyed");
if (notifications && manager != null) {
Download[] remaining = (Download[]) intent.getParcelableArrayExtra(Commons.ARGS.DATA);
if (remaining == null) break;
for (Download d : remaining) {
// Download has failed since service was destroyed; was never called and has no relations to this case. manager.notify() is called here to update the notification as "cancelled" btw
}
}
break;
case Commons.ARGS.ERR_LOAD:
Log.w(TAG, "Failed to load queue");
break;
}
if (data == null) return;
if (notifications) {
Intent i = new Intent(context, MainActivity.class);
NotificationCompat.Builder builder = new NotificationCompat.Builder(activity, Commons.Notif.DOWNLOAD_PROGRESS)
.setContentIntent(PendingIntent.getActivity(activity, 0, i, 0))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSmallIcon(R.drawable.ic_notif)
.setContentTitle(data.title)
.setColor(context.getResources().getColor(R.color.Accent))
.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY)
.setGroup(CHANNEL_ID);
if (manager == null) {
manager = (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Downloads in Progress", NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("All downloads are displayed here");
channel.enableLights(false);
channel.enableVibration(false);
manager.createNotificationChannel(channel);
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId(CHANNEL_ID);
}
// Notification builder styling is handled here
manager.notify(0, new NotificationCompat.Builder(activity, Commons.Notif.DOWNLOAD_PROGRESS)
.setChannelId(CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notif)
.setGroup(CHANNEL_ID)
.setColor(activity.getResources().getColor(R.color.Accent))
.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY)
.setGroupSummary(true)
.setContentText("All downloads have finished.")
.setContentTitle("Done")
.build());
manager.notify(DOWNLOAD_BASE_ID | (data.id & 0x00FF_FFFF), builder.build());
Log.d(TAG, "Notification updated");
}
}
Thanks in advance :P

I do not know what exactly is happening but canceling the notification using manager.cancel() before updating it with the success/final notification solved the problem.
int id = // Notification id
if (success) {
Log.d(TAG, "Success Notification updated");
manager.cancel(id);
} else Log.d(TAG, "Notification updated");
manager.notify(id, builder.build());

Related

Load Fragment on FCM Push Notification click

I am using pyFCM on a remote server to push event notifications to my app. The notification has a data payload as well, and this is needed for making some UI changes.
When the server app sends the app a notification, there is a notification that pops up on the phone, pressing which causes the default activity to be opened. Also, I have overridden the onMessageReceived in the service extending FirebaseMessagingService, and the data payload is then taken from the message and processed.
I have seen multiple questions on SO like these:
How to open Fragment on click of push notification
Open fragment from notification when the app in background
How to open fragment page, when pressed a notification in android
Open a fragment when press a notification in android
One thing in common with all the above SO questions is that they involve building a notification using Notification.Builder, and then setting up an Activity to open (or it's onNewIntent to be triggered depending on whether it's android:launchMode is "singleTop" in the Manifest ) . My distinct understanding is that this would work if FCM were used to send the app a data message and not a notification.
The question(s)
What I really fail to understand is how setting up a notification after the data is received from FCM helps me change the behaviour of the notification received from FCM's push. Is that even possible, or have I gotten something wrong?
The second part to this question is: How do I handle a user click on a push notification to load a specific fragment in my activity (when I am not the one creating the fragment in the app - just received via FCM)?
This is the current code that runs in my app.
The onMessageReceived code is :
#Override
public void onMessageReceived(RemoteMessage remoteMessagenew) {
this.remoteMessage = remoteMessagenew;
super.onMessageReceived (remoteMessage);
database = MyDatabase.getDatabase(getApplication());
appuser = database.AppDao().getUserDetails();
Log.d(TAG, "onMessageReceived: ");
Log.d (TAG, "onMessageReceived: #########################################################");
Log.d (TAG, "onMessageReceived: #########################################################");
Log.d (TAG, "onMessageReceived: Message Data is : " + remoteMessage.getData ());
Map messageData = remoteMessage.getData ();
// Broadcast
args = new Bundle ();
int stage = 0 ;
Log.d (TAG, "onMessageReceived: ");
if(messageData.containsKey ("otherid")) {
incidenthandler(messageData);
} else if(messageData.containsKey ("itemid")) {
itemhandler(messageData);
}
}
The item handler to handle the data to refresh on the app screen
private void itemhandler(Map messageData) {
messagebody = remoteMessage.getNotification().getBody();
if(messageData.containsKey("itemid")) {
Log.d(TAG, "itemhandler: #######################");
Log.d(TAG, "itemhandler: #######################");
Log.d(TAG, "itemhandler: UPDATING");
Log.d(TAG, "itemhandler: #######################");
Log.d(TAG, "itemhandler: #######################");
String item = (String ) messageData.get("itemid");
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = (JsonObject)jsonParser.parse(item);
item refreshedItem = parseFromJson(jsonObject);
database.revivDao().upsert(refreshedItem);
} else {
// this really shouldn't happen, but putting in a scenario where this does
// syncing the db with the app
Log.d(TAG, "itemhandler: #######################");
Log.d(TAG, "itemhandler: #######################");
Log.d(TAG, "itemhandler: REFRESHING ");
Log.d(TAG, "itemhandler: #######################");
Log.d(TAG, "itemhandler: #######################");
// this triggers a call to my intentservice to refresh the db
Log.d(TAG, "itemhandler: refreshing items");
Intent intent = new Intent ("refreshitems");
Bundle clickdata = new Bundle();
data.putString("item_sub1", item_sub1);
data.putString("item_sub1", item_sub1);
intent.putExtra("data", clickdata) ; // add Bundle to Intent
localBroadcastManager.getInstance(getApplication()).sendBroadcast(intent); // Broadcast Intent
}
String itemid = messageData.get("itemid").toString();
try{
Log.d(TAG, "itemhandler: #######################");
Log.d(TAG, "itemhandler: #######################");
Log.d(TAG, "itemhandler: sending localbroadcast");
Log.d(TAG, "itemhandler: #######################");
Log.d(TAG, "itemhandler: #######################");
// this is where I try and switch to the screen to display some info - if the app is running
Intent notificationIntent = new Intent("switchtofragment");
notificationIntent.putExtra("launchitemfragment", true);
notificationIntent.putExtra("itemid", itemid);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
if(localBroadcastManager == null) localBroadcastManager = LocalBroadcastManager.getInstance(getApplication());
localBroadcastManager.sendBroadcast(notificationIntent);
// now override the notification to load the fragment on click
setupNotification(itemid, messagebody);
notificationManager.notify(1, builder.build());
} catch (NullPointerException e){
return;
}
}
Finally, the function to handle the notification:
private void setupNotification(String housecallid, String message) {
//Log.d(TAG, "setting up notification");
String idChannel = ANDROID_CHANNEL_ID;
Context context = getApplicationContext();
Intent notificationIntent = new Intent(getApplicationContext(), Reviv.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.putExtra("housecallid", housecallid);
notificationIntent.putExtra("launchhousecallfragment", true);
Bundle extras = new Bundle();
extras.putBoolean("launchhousecallfragment", true);
extras.putString("housecallid", housecallid);
notificationIntent.putExtras(extras);
notificationIntent.putExtra("data", extras);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
if(notificationManager == null )
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
builder = new NotificationCompat.Builder(context, null);
builder. setSmallIcon(R.drawable.heart)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
R.drawable.heart))
.setContentIntent(pendingIntent)
.setContentTitle("Reviv")
.setContentText(message);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if(notificationChannel == null) {
notificationChannel = new NotificationChannel(idChannel, context.getString(R.string.app_name), importance);
// Configure the notification channel.
notificationChannel.setDescription("Reviv Housecall Notification");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notificationManager.createNotificationChannel(notificationChannel);
builder.setChannelId(idChannel);
builder.setAutoCancel(true);
}
} else {
builder.setContentTitle(context.getString(R.string.app_name))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setColor(ContextCompat.getColor(context, R.color.transparent))
.setVibrate(new long[]{100, 250})
.setLights(Color.RED, 500, 5000)
.setAutoCancel(true);
}
builder.setContentIntent(pendingIntent);
}
You could rather send data messages only instead of sending push messages + payload and receive all notifications in onMessageReceived whether the app is in foreground or background. You can then setup the notification using the setupNotification function you have written and check for extras in the start of the Reviv activity and replace fragments accordingly.
I'm not sure but about this approach using push notifications but here's a suggestion: Try to see if you receive the data payload (check for getIntent().getExtras()) which you are sending with the push notification in the activity which is opened by default on clicking the push notification.

How to handle notifications with FCM when app is in either foreground or background

I used firebase to build My project.
It will also use the FCM (firebase cloud message).
But there is a problem. I can't handle the FCM (create my custom notificaion) when app is in background.
The official site tutorial said that
case 1: App foreground -> override the "onMessageReceived()" to create your custom notification.
case 2: App background -> System will create the notification directly. We needn't and can't do anything. Because it doesn't trigger the "onMessageReceived()" in this case.
However if I can do nothing when app is background, I can't create my custom notification. (e.g. After Users click the notification and it will pop up a window to show detail information.)
So how do I handle notifications with FCM when app is in background?
There is a bad news. Google change the Firebase source code in version 'com.google.firebase:firebase-messaging:11.6.0'.
handelIntent is "public final void method" now. which means we can't override it .
If you want to use the solution, change the version to be "com.google.firebase:firebase-messaging:11.4.2"
Try my way. It can perfectly work on the project build version is Android 6.0 above(api level 23) and I have tried it already.
There is better way than official site tutorial
The official site said that the notification will be created by system when app is in background. So you can't handle it by overriding the "onMessageReceived()". Because the "onMessageReceived()" is only triggered when app is in foreground.
But the truth is not. Actually the notificaions (when app is in background) are created by Firebase Library.
After I traced the firebase library code. I find a better way.
Step 1. Override the "handleIntent()" instead of "onMessageReceived()" in FirebaseMessagingService
why:
Because the method will be trigger either app is in foreground or the background. So we can handle FCM message and create our custom notifications in both cases.
#Override
public void handleIntent(Intent intent) {
Log.d( "FCM", "handleIntent ");
}
Step 2. Parse the message from FCM
how:
If you don't know the format of the message you set. Print it and try to parse it.
Here is the basic illustration
Bundle bundle = intent.getExtras();
if (bundle != null) {
for (String key : bundle.keySet()) {
Object value = bundle.get(key);
Log.d("FCM", "Key: " + key + " Value: " + value);
}
}
Step 2. Remove the notifications created by Firebase library when the app is in background
why:
We can create our custom notification. But the notification created by Firebase Library will still be there (Actually it created by ""super.handleIntent(intent)"". There is detail explaination below.). Then we'll have two notifcations. That is rather weird. So we have to remove the notificaion created by Firebase Library
how (project build level is Android 6.0 above):
Recognize the notifications which we want to remove and get the informaion. And use the "notificationManager.cancel()" to remove them.
private void removeFirebaseOrigianlNotificaitons() {
//check notificationManager is available
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager == null )
return;
//check api level for getActiveNotifications()
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
//if your Build version is less than android 6.0
//we can remove all notifications instead.
//notificationManager.cancelAll();
return;
}
//check there are notifications
StatusBarNotification[] activeNotifications =
notificationManager.getActiveNotifications();
if (activeNotifications == null)
return;
//remove all notification created by library(super.handleIntent(intent))
for (StatusBarNotification tmp : activeNotifications) {
Log.d("FCM StatusBarNotification",
"StatusBarNotification tag/id: " + tmp.getTag() + " / " + tmp.getId());
String tag = tmp.getTag();
int id = tmp.getId();
//trace the library source code, follow the rule to remove it.
if (tag != null && tag.contains("FCM-Notification"))
notificationManager.cancel(tag, id);
}
}
The my whole sample code:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static int notificationCount=0;
#Override
public void handleIntent(Intent intent) {
//add a log, and you'll see the method will be triggered all the time (both foreground and background).
Log.d( "FCM", "handleIntent");
//if you don't know the format of your FCM message,
//just print it out, and you'll know how to parse it
Bundle bundle = intent.getExtras();
if (bundle != null) {
for (String key : bundle.keySet()) {
Object value = bundle.get(key);
Log.d("FCM", "Key: " + key + " Value: " + value);
}
}
//the background notification is created by super method
//but you can't remove the super method.
//the super method do other things, not just creating the notification
super.handleIntent(intent);
//remove the Notificaitons
removeFirebaseOrigianlNotificaitons();
if (bundle ==null)
return;
//pares the message
CloudMsg cloudMsg = parseCloudMsg(bundle);
//if you want take the data to Activity, set it
Bundle myBundle = new Bundle();
myBundle.putSerializable(TYPE_FCM_PLATFORM, cloudMsg);
Intent myIntent = new Intent(this, NotificationActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
myIntent.putExtras(myBundle);
PendingIntent pendingIntent = PendingIntent.getActivity(this, notificationCount, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//set the Notification
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.icon)
.setContentTitle(cloudMsg.getTitle())
.setContentText(cloudMsg.getMessage())
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationCount++, notificationBuilder.build());
}
/**
* parse the message which is from FCM
* #param bundle
*/
private CloudMsg parseCloudMsg(Bundle bundle) {
String title = null, msg=null;
//if the message is sent from Firebase platform, the key will be that
msg = (String) bundle.get("gcm.notification.body");
if(bundle.containsKey("gcm.notification.title"))
title = (String) bundle.get("gcm.notification.title");
//parse your custom message
String testValue=null;
testValue = (String) bundle.get("testKey");
//package them into a object(CloudMsg is your own structure), it is easy to send to Activity.
CloudMsg cloudMsg = new CloudMsg(title, msg, testValue);
return cloudMsg;
}
/**
* remove the notification created by "super.handleIntent(intent)"
*/
private void removeFirebaseOrigianlNotificaitons() {
//check notificationManager is available
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager == null )
return;
//check api level for getActiveNotifications()
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
//if your Build version is less than android 6.0
//we can remove all notifications instead.
//notificationManager.cancelAll();
return;
}
//check there are notifications
StatusBarNotification[] activeNotifications =
notificationManager.getActiveNotifications();
if (activeNotifications == null)
return;
//remove all notification created by library(super.handleIntent(intent))
for (StatusBarNotification tmp : activeNotifications) {
Log.d("FCM StatusBarNotification",
"tag/id: " + tmp.getTag() + " / " + tmp.getId());
String tag = tmp.getTag();
int id = tmp.getId();
//trace the library source code, follow the rule to remove it.
if (tag != null && tag.contains("FCM-Notification"))
notificationManager.cancel(tag, id);
}
}
}
However if I can do nothing when app is background, I can't create my custom notification. (e.g. After Users click the notification and it will pop up a window to show detail information.)
So how do I handle notifications with FCM when app is in background?
First, you need to create correct message payload that you send to fcm server. Example:
{
"to": "topic_name",
"priority": "high",
"data": {
"field1": "field1 value"
"field2": "field2 value"
}
"notification" : {
"body" : "Lorem ipsum",
"title" : "sampke title"
"click_action": "SHOW_DETAILS"
}
}
data payload is actual data you want to show as message details after user clicks on notification, notification payload represents how generated notification should look (there are much more attributes possible to set), you don't need to build notification by yourself, you only need to set it properties here.
To show your activity after user taps on notication, you need to set intent filter corresponding to click_action:
<intent-filter>
<action android:name="SHOW_DETAILS"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
so activity that have above intent filter will be launched automatically when user taps to notification.
Last step is to retrieve data when activity is launched after notification tap. It's pretty easy. Custom data is passed to activity via bundle. Inside onCreate method for your activity do something like that:
Bundle bundle = getIntent().getExtras();
if(bundle.getString("action").equals("SHOW_DETAILS")) /*This indicates activity is launched from notification, not directly*/
{
//Data retrieved from notification payload send
String filed1 = bundle.getString("field1");
String filed2 = bundle.getString("field2");
}
All of above is valid if app is not running or it's in background. If your app is foreground, no notification will be created. Instead, you will receive onMessageReceived() event so you can handle the same data there (I guess you know how).
Reference:
https://firebase.google.com/docs/cloud-messaging/http-server-ref
https://github.com/firebase/quickstart-android/tree/master/messaging
You need to use FCM data messages in order to create custom notification in a android app.Even your app is in background, onMessageReceived will be called, so you can process the data and show the custom notification.
https://firebase.google.com/docs/cloud-messaging/android/receive
Data message format which has to be sent from server:
{"message":{
"token":"Your Device Token",
"data":{
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
}
}
}
FCM Won't send a background notification if your app is killed any more, and as you described in your answer about the handleIntent() solution It may work for some devices and for some old version of the FCM, also if you #override method that doesn't described in the official doc's of firebase you may struggle some problems here, and you use it on your own risk!.
What is the solution?
You need to use your own push-notification-service beside FCM like Telegram.
OR using SyncAdapter beside GCM like Gmail.
So if you need it to work successfully like those apps, you have to use your own hack.
public class FirebaseMessageReceiver extends FirebaseMessagingService{
private static final String TAG = "main";
String s12;
String channel_id = "general";
Intent intent;
#Override
public void onNewToken(#NonNull String token)
{
Log.d(TAG, "Refreshed token: " + token);
}
#Override
public void
onMessageReceived(RemoteMessage remoteMessage) {
s12=remoteMessage.getNotification().getClickAction();
Log.d("tttt",(remoteMessage.getData().toString()));
Log.d("ttttttt",(remoteMessage.getNotification().toString()));
if (remoteMessage.getNotification() != null) {
showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}
//
}
public void handleIntent(Intent intent)
{
try
{
if (intent.getExtras() != null)
{
RemoteMessage.Builder builder = new RemoteMessage.Builder("FirebaseMessageReceiver");
for (String key : intent.getExtras().keySet())
{
builder.addData(key, intent.getExtras().get(key).toString());
}
onMessageReceived(builder.build());
}
else
{
super.handleIntent(intent);
}
}
catch (Exception e)
{
super.handleIntent(intent);
}
}
private RemoteViews getCustomDesign(String title, String message) {
RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification);
remoteViews.setTextViewText(R.id.title111, title);
remoteViews.setTextViewText(R.id.message111, message);
remoteViews.setImageViewResource(R.id.icon111, R.drawable.favicon);
return remoteViews;
}
// Method to display the notifications
public void showNotification(String title, String message) {
intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(s12));
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent notifyIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
Log.d("notifyy",notifyIntent.toString());
NotificationCompat.Builder builder
= new NotificationCompat
.Builder(getApplicationContext(),
channel_id)
.setSmallIcon(R.drawable.favicon)
.setAutoCancel(true)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.setOnlyAlertOnce(true)
.setContentIntent(notifyIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
builder = builder.setContent(getCustomDesign(title, message));
}
else {
builder = builder.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.drawable.favicon);
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Check if the Android Version is greater than Oreo
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(channel_id, "web_app",
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(
notificationChannel);
}
notificationManager.notify(0, builder.build());
}
}

Notification small icon is not updating as expected

I am using foregroundService for playing audio in Background and show a notification with actions.
public NotificationHelper(ForegroundService service) {
mFG_Service = service;
mNotificationManager = (NotificationManager) mFG_Service.getSystemService(Context.NOTIFICATION_SERVICE);
}
public void showNotification(ChaptersBO chaptersBO, boolean isPlaying) {
Notification notification = createNotification(chaptersBO, isPlaying);
if (notification != null) {
if (isPlaying) {
mFG_Service.startForeground(NOTIFICATION_ID.FOREGROUND_SERVICE, notification);
} else {
mFG_Service.stopForeground(false);
mNotificationManager.notify(NOTIFICATION_ID.FOREGROUND_SERVICE, notification);
}
}
}
private Notification createNotification(ChaptersBO chapter, boolean isPlaying) {
if (chapter == null)
return null;
NotificationCompat.Builder builder = new NotificationCompat.Builder(mFG_Service);
try {
Bitmap sourceBitmap = BitmapFactory.decodeResource(mFG_Service.getResources(), R.drawable.notification_logo);
Bitmap resizedBitmap = Bitmap.createScaledBitmap(sourceBitmap, 300, 300, false);
if (resizedBitmap != sourceBitmap) {
sourceBitmap.recycle();
}
builder.setContentTitle("Audio")
.setContentText(chapter.getChapterNumber())
.setPriority(Notification.PRIORITY_DEFAULT)
.setSmallIcon(getSmallIcon(isPlaying))
.setLargeIcon(resizedBitmap)
.setShowWhen(false)
.setOngoing(isPlaying)
.setContentIntent(getPendingIntent())
.addAction(R.drawable.ic_notif_prev, "", getAction(ACTION.PREV_ACTION, REQ_CODE_PREVIOUS))
.addAction(getSmallIcon(isPlaying), "", getAction(ACTION.PLAY_PAUSE_ACTION, REQ_CODE_PLAY_OR_PAUSE))
.addAction(R.drawable.ic_notif_next, "", getAction(ACTION.NEXT_ACTION, REQ_CODE_NEXT))
.setDeleteIntent(getStopIntent())
.setStyle(new NotificationCompat.DecoratedCustomViewStyle());
} catch (Exception e) {
e.printStackTrace();
}
return builder.build();
}
I want to update notification small icon and center action icon on play/pause. For the first time it works fine but if I clear the notification and play another audio then these icons (Encircled in below screenshot) are not updating.
To pause audio I am calling this:
mNotificationManager.notify(NOTIFICATION_ID.FOREGROUND_SERVICE, notification);
If I replace it with this
mFG_Service.startForeground(NOTIFICATION_ID.FOREGROUND_SERVICE, notification);
then it works fine but in that case I can't clear notification.
Any suggestions to update these icons on Play/Pause and also make the notification removable when audio is paused will be appreciated.

Change Notification's action icon dynamically

I'm trying to create a notification for my app that has an action that I can change its icon, title and intent when I click on it.
I see its possible here at 3:33
https://www.youtube.com/watch?v=tKoQatxG0_8&index=73&list=PLOU2XLYxmsIIwGK7v7jg3gQvIAWJzdat_
But I couldn't find a way to do it.
also, If someone know how to use the pause/play icon on the Right watch in the link, i'll like to know that also.
Thanks from advance.
You can access notification actions from notificationObj.actions
Try below: (Note: below code is not complete, but it will give you an idea on how to change action icon)
Notification status = null;
private NotificationCompat.Builder mBuilder;
private NotificationManager mManager;
private final int STATUS_ID = 1;
private String CHANNEL_ID = "channel_name";
private void setUpNotification() {
mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int playOrPause;
if(isPlaying()) {
playOrPause = R.drawable.ic_pause;
} else {
playOrPause = R.drawable.ic_play;
}
if(mBuilder == null) {
//Setup Builder
mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
mBuilder.setOngoing(true)
.addAction(playOrPause, "play", pendingIntent1) // Index 0
.addAction(R.drawable.ic_2, "text2", pendingIntent2) // Index 1
.addAction(R.drawable.ic_3, "text3", pendingIntent3);// Index 2
status = mBuilder.build();
} else {
//Update builder as per your needs
mBuilder.setContentTitle(title);
status = mBuilder.build();
status.actions[0] = new Notification.Action(playOrPause, "play", pendingIntent1);
}
startForeground(STATUS_ID, status);
}
I was able to solve the same problem by accessing builder.mActions. It's an ArrayList of all the actions you've added. I modify this without recreating the builder and calling build seems to update this.
This doesn't seem to be documented in the SDK but I'm ok with that for now.
Will let you know if I come up with something else
By following #Sammer J suggestion I came up with the following solution.
try {
// mBuilder is a Notification.Builder object.
Field field = mBuilder.getClass().getDeclaredField("mActions");
field.setAccessible(true);
ArrayList<Notification.Action> mActions = (ArrayList<Notification.Action>) field.get(mBuilder);
if (mActions != null) {
mActions.set(0, new Notification.Action(R.drawable.ic_action_resume, getString(R.string.button_resume), pendingIntent));
field.set(mBuilder, mActions);
}
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
mNotificationManager.notify(ONGOING_NOTIFICATION_ID, mBuilder.build());

Click on notification to enter my app in android

Currently I am working on GCM (Google Cloud message), it allow user to push the message to user device. And I would like achieve the following requirement :
if the user has already enter app , ignore it
if the user has not enter the app , click on notification to enter the app
And the work flow of my app is:
WelcomePage (download json and create data set from it) => MainPage (Display base on the data set)
The code to handle notification
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
String notifyMsg = "";
JSONTokener tokener = new JSONTokener(msg);
if (tokener != null) {
try {
notifyMsg = new JSONObject(tokener).getString("msg");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Intent myintent = new Intent(this, WelcomePageActivity.class);
myintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, myintent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getResources().getString(R.string.notification_title))
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(notifyMsg))
.setContentText(notifyMsg)
.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
The problem is if I use WelcomePageActivity class , it will create a new activity if I am at the main page, how can I adjust the code to fit my requirement ?
Thanks
For
1. if the user has already enter app , ignore it:
in the onReceive() , check if your app is running, do not notify.
It can be checked with something like:
ActivityManager activityManager =(ActivityManager)gpsService.this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> serviceList= activityManager.getRunningServices(Integer.MAX_VALUE);
if((serviceList.size() > 0)) {
boolean found = false;
for(int i = 0; i < serviceList.size(); i++) {
RunningServiceInfo serviceInfo = serviceList.get(i);
ComponentName serviceName = serviceInfo.service;
if(serviceName.getClassName().equals("Packagename.ActivityOrServiceName")) {
//Your service or activity is running
break;
}
}
if the user has not enter the app , click on notification to enter the app
from the code above, you'l know if you would like to resume the app or launch - call Splash Screen or in your case WelcomeActivity.
About the workflow of your app, i'd suggest check whether you need to download the data every time or not. Can save it maybe or update/download only when required, and rest of flow works as it is.
In your AndroidManifest.xml, define your WelcomePageActivity with the flag android:launchMode="singleTop". From the definition of this flag:
A new instance of a "singleTop" activity may also be created to handle
a new intent. However, if the target task already has an existing
instance of the activity at the top of its stack, that instance will
receive the new intent (in an onNewIntent() call); a new instance is
not created.
So with this flag, your activity will not be created again, rather it will receive a call in the onNewIntent() function with the Intent you used to create the PendingIntent for the notification. You could override this function, and use the intent to pass the activity new information.
You will not able to receive any notification click event so,
try this code :
Intent myintent = new Intent(this, TestActivity.class);
myintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, myintent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getResources().getString(R.string.notification_title))
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(notifyMsg))
.setContentText(notifyMsg)
.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
public class TestActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// check for your app state is running or not
if(appRunning == false) {
// start your WelcomePage activity.
}
}
}
1.Create an object in GcmIntentService
public static final Object CURRENTACTIVIYLOCK = new Object();
//for storing current activity
public static Activity currentActivity;
2.Update this object value in onPause and onResume of MainActivity to recognize Activity is running or not.
#Override
public void onResume() {
super.onResume();
System.out.println("onResume Home page");
synchronized (GcmIntentService.CURRENTACTIVIYLOCK) {
GcmIntentService.currentActivity = this;
}
}
#Override
public void onPause() {
super.onPause();
synchronized (GcmIntentService.CURRENTACTIVIYLOCK) {
GcmIntentService.currentActivity = null;
}
}
3.In GcmIntentService class, check for the current activity in onHandleIntent method.
synchronized (CURRENTACTIVIYLOCK) {
if (currentActivity != null) {
if (currentActivity.getClass() == HomePageActivity.class) {
} else {
sendNotification(extras.getString("message"));
}
} else {
sendNotification(extras.getString("message"));
}
I'm sure this will help you.

Categories

Resources