Reputation: 39
Currently I am sending notification as:
{
"data" : {
"sound": "default",
"icon": "ic_app_launcher",
"..." : "..."
.
.
.
}
}
E/fcmbundle: Dumping Intent start
E/fcmbundle: [google.sent_time=...]
E/fcmbundle: [from=...]
E/fcmbundle: [icon=ic_app_launcher]
E/fcmbundle: [sound=default]
E/fcmbundle: [google.message_id=0:...]
E/fcmbundle: [collapse_key=...]
E/fcmbundle: Dumping Intent end
This is Log which data I get from FCM when the app in background, and on click of the notification it will open the launcher Activity.
I have read a number of answers relating to the same issue but do not work for me.I am sending the data-message and inside that this is received. Sound and icon also. But the do not work. There is no notification sound and icons is shown as a square.
I have also tried the single notification in json as:
{
"to" : "...",
"notification" : {
"sound": "default",
"icon" : "myicon"
},
"data" : {}
}
But this also does not work as it should.
The notification comes in background but does not make a sound and neither shows the icon.
When the app is in foreground the notification works properly showing icon, notifies with sound, as required.
Can anyone please help me with this?
Upvotes: 1
Views: 2372
Reputation: 1789
For notification in both foreground and background, Consider using data
only payload. then override
onMessageReceived
method in your custom MessageService class
Below is a solution that works fine for me.
notificationPayload = {"data": { "title": "your title" ,"body": "Your body"}}
public class CustomFirebaseMessagingService extends FirebaseMessagingService {
final String CHANNEL_ID = "YOUR_CHANNEL_ID";//when targeting android-o
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if (remoteMessage.getData().size() > 0) {
sendNotification(remoteMessage);
}
}
private void sendNotification(RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
String title = data.get("title");
String message = data.get("body");
Intent resultIntent = new Intent(this, YOUR_TARGET_ACTIVITY.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent resultPendingIntent = PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_ONE_SHOT
);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setAutoCancel(true);
int notificationId = (int) System.currentTimeMillis();
mBuilder.setSound(alarmSound);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (manager != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
"Human Readable channel id",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setShowBadge(true);
manager.createNotificationChannel(channel);
}
manager.notify(notificationId, mBuilder.build());
}
}
}
Upvotes: 3
Reputation: 1978
try this:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage != null && remoteMessage.getData().size() > 0) {
try {
JSONObject json = new JSONObject(remoteMessage.getData().toString());
sendPushNotification(json);
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
}
sendPushNotification method
private void sendPushNotification(JSONObject json) {
try {
//getting the json data
JSONObject data = json.getJSONObject("data");
//creating MyNotificationManager object
MyNotificationManager mNotificationManager = new MyNotificationManager(getApplicationContext());
//creating an intent for the notification
mNotificationManager.showSmallNotification(title, message, intent);
OR
mNotificationManager.showBigNotification(title, message, imageUrl, intent);
} catch (JSONException e) {
Log.e(TAG, "Json Exception: " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
MyNotificationManager class :
public class MyNotificationManager {
public int ID_BIG_NOTIFICATION = 234;
public int ID_SMALL_NOTIFICATION = 235;
private Context mCtx;
public MyNotificationManager(Context mCtx) {
this.mCtx = mCtx;
ID_BIG_NOTIFICATION = getRandom();
ID_SMALL_NOTIFICATION = getRandom();
}
private int getRandom() {
Random random = new Random();
return random.nextInt(99999) + 1;
}
//the method will show a big notification with an image
//parameters are title for message title, message for message text, url of the big image and an intent that will open
//when you will tap on the notification
public void showBigNotification(String title, String message, String url, Intent intent) {
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mCtx,
ID_BIG_NOTIFICATION,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
bigPictureStyle.setBigContentTitle(title);
bigPictureStyle.setSummaryText(Html.fromHtml(message).toString());
bigPictureStyle.bigPicture(getBitmapFromURL(url));
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx);
Notification notification;
notification = mBuilder.setSmallIcon(R.mipmap.ic_launcher).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentIntent(resultPendingIntent)
.setContentTitle(title)
.setStyle(bigPictureStyle)
.setDefaults(Notification.DEFAULT_VIBRATE|Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS)
.setSmallIcon(R.drawable.ic_app_icon)
.setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.drawable.ic_app_icon))
.setContentText(message)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(ID_BIG_NOTIFICATION, notification);
}
//the method will show a small notification
//parameters are title for message title, message for message text and an intent that will open
//when you will tap on the notification
public void showSmallNotification(String title, String message, Intent intent) {
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mCtx,
ID_SMALL_NOTIFICATION,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx);
Notification notification;
notification = mBuilder.setSmallIcon(R.mipmap.ic_launcher).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentIntent(resultPendingIntent)
.setContentTitle(title)
.setSmallIcon(R.drawable.ic_app_icon)
.setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.drawable.ic_app_icon))
.setContentText(message)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(ID_SMALL_NOTIFICATION, notification);
}
//The method will return Bitmap from an image URL
private Bitmap getBitmapFromURL(String strURL) {
try {
URL url = new URL(strURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
Upvotes: 0