Reputation: 429
I am implementing GCM service in my android app and i am getting notification also.But we get problem when my app is closed or in background.
When app is in foreground then everything is working fine, I am getting notification with all text and icon but when my app is in background, we get notification text and title but icon is not visible. I searched about this and reached the conclusion that notification is handled by device notification tray when your app is in background.
Here is my code to receive notification:
public class GCMPushReceiverService extends GcmListenerService {
//This method will be called on every new message received
@Override
public void onMessageReceived(String from, Bundle data) {
//Getting the message from the bundle
String message = data.getString("message");
Log.d("data",data.toString());
//Displaying a notiffication with the message
String body = null;
String title = null;
try{
String data1 = data.toString();
String json = (data1.split("notification=Bundle\\[")[1]).split("\\]")[0];
body = (json.split("body\\=")[1]).split("\\,")[0];
// title = (((json.split("body\\=")[1]).split("\\,")[1]).split("title\\=")[1]).split("\\,")[0];
title = (((json.split("body\\=")[1]).split("vibrate")[0]).split("title=")[1]).split(",")[0];
Log.d("json",json);
JSONObject notificationJSON = new JSONObject(json);
//String notificationJSONString = data.getString("notification");
//then you can parse the notificationJSONString into a JSON object
// JSONObject notificationJSON = new JSONObject(notificationJSONString );
// body = notificationJSON.getString("body");
//title = notificationJSON.getString("title");
Log.d("body",body);
Log.d("title",title);
}catch (Exception e){
e.printStackTrace();
}
// sendNotification(message);
sendNotification(body, title);
}
//This method is generating a notification and displaying the notification
private void sendNotification(String message,String titles) {
Intent intent = new Intent(this, NavigationDrawerActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("firsttab","notify");
int requestCode = 0;
int number = 0;
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this)
// .setSmallIcon(R.mipmap.philips_launcher)
.setSmallIcon(getNotificationIcon())
.setContentTitle(titles)
.setContentText(message)
.setAutoCancel(true)
.setSound(sound)
.setNumber(++number)
.setColor(Color.parseColor("#0089C4"))
// .setStyle(inboxStyle)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setStyle(new NotificationCompat.BigTextStyle().bigText(titles))
.setContentIntent(pendingIntent);
/* if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setSmallIcon(R.drawable.icon_transperent);
} else {
builder.setSmallIcon(R.drawable.icon);
}*/
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, noBuilder.build()); //0 = ID of notification
}
private int getNotificationIcon() {
boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
return useWhiteIcon ? R.drawable.notification_icon : R.drawable.not_icon;
}
}
My question is how to handled notification when my app is in background? And how to show notification icon when app is in background? When I clicked on notification it open launcherActivity but I want to open some otherActivity.
Upvotes: 0
Views: 1484
Reputation: 394
If you are using FCM, Add this in your app Manifest:
<!-- Set custom default icon. This is used when no icon is set for incoming notification messages. -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_stat_ic_notification" />
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
notification message. -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
Only then the custom color & icon will be shown in the notification when the app is in background.
Upvotes: 0
Reputation: 9225
It looks like you are sending a notification message, notification messages are delivered to onMessageReceived callback when the app is in the foreground like you are seeing. However when the app is in the foreground notification messages are NOT passed to onMessageReceived, the notification payload of the message is used to automatically display a notification. If there is an accompanying data payload that payload will be available in the extras of the launched intent when the user taps the notification.
If you want complete control of how messages are handled then you should use data messages, which are always delivered to the onMessageReceived callback.
See more on the different ways the two types of FCM messages are handled here.
Note: - that if you are using the Firebase console to send the messages, it only supports notification messages at this time. Even if you add custom data, it will still be treated as a notification message on the client side. - If you are using the REST API to send the notification message you can specify the click_action field to determine which Activity will be launched when the user clicks on the notification.
Upvotes: 0
Reputation: 13469
Based from this thread, when app is in the background, it needs server side to make a action. The data part will automatically saved in a intent and send to the activity contains that action in the content filter. Stated in this related SO question that notification messages automatically generate notifications based on the properties passed in the "notification" object of your downstream message request, however onMessageReceived
is not called in this case. Check this tutorial. When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification.
You can also set the priority to high when sending the message. It allows the GCM service to wake a sleeping device when possible and open a network connection to your app server.
You can check on these related links:
Hope this helps!
Upvotes: 1