I am trying to get push notifications using FCM in my android application the code is working fine when notification is sent from FCM console it works fine both in foreground and background, but when I send notifications using curl through FCM I am getting data which is visible in logcat but notification is displayed only when application is in background and does not show up when application is in foreground. onMessageReceived is called every time when data is pushed from FCM but notification is not visible when application is in foreground.
private void sendNotification(String body , Map<String,String> data) {
int finalSecId = Integer.parseInt((String) data.get("sec_id"));
int sec = Integer.parseInt((String) data.get("sec"));
String extra1 = (String) data.get("extra1");
String extra2 = (String) data.get("extra2");
Intent intent = new Intent(this,HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0/*Request code*/, intent, PendingIntent.FLAG_ONE_SHOT);
//Set sound of notification
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notifiBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.login_meter)
.setContentTitle(getString(R.string.app_name))
.setContentText(body)
.setAutoCancel(true)
.setSound(notificationSound)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /*ID of notification*/, notifiBuilder.build());
}
Related
I'm using fcm for push notification.
when app is open and notification received , notification work correctly.
but when app is closed and notification is recieved, some features like vibrate , sound , addaction , ... not working.
Intent intent = new Intent(this, FullscreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
String urlAddress = "https://cafebazaar.ir/";
Intent intent2 = new Intent(Intent.ACTION_VIEW, Uri.parse(urlAddress));
intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent2.setPackage("com.farsitel.bazaar");
PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 0, intent2, PendingIntent.FLAG_ONE_SHOT);
Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification notificationBuilder = new Notification.Builder(this)
.setSmallIcon(R.drawable.macanads)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setColor(Integer.parseInt(color))
.setSound(soundUri)
.setVibrate(new long[] { 1000, 1000, 1000})
.addAction(android.R.drawable.ic_menu_directions,"بروزرسانی",pendingIntent2)
.setContentIntent(pendingIntent).build();
}
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder);
}
My problem has been solved.you must not put JSON key 'notification' in your request to firebase API but instead use 'data'
To make firebase library to call your onMessageReceived() in the following cases
App in foreground
App in background
App has been killed
you must not put JSON key 'notification' in your request to firebase API but instead use 'data', see below.
The following message will not call your onMessageReceived() when your app is in the background or killed, and you can't customize your notification.
When my broadcast receiver is triggered it will launch a notification that directs the user to an chat activity which is build in the application.
The problem is when the user is inside the application doing things. (it's main purpose is not chatting)
let's say receives the notification while he's in the menu of the app
When he presses the notification the chat activity starts up
When pressing the back button the user goes to the home screen of the phone?
I want it to go back to the activity where he triggered the notification. This could be anywhere inside the application.
Building the notification:
notificationIntent = new Intent(mContext, BerichtenActivity.class);
Toast.makeText(context, verzender + ": " + inhoud, Toast.LENGTH_LONG).show();
Notification notification = NotificationUtils.handleNotification(mContext, notificationIntent, verzender, inhoud, R.drawable.ic_stat_chat);
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, notification);
public static Notification handleNotification(Context context, Intent resultIntent, String titel, String body, int smallIcon){
PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, 0);
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setAutoCancel(true)
.setContentText(body)
.setContentTitle(titel)
.setContentIntent(resultPendingIntent)
.setSound(uri)
.setLargeIcon(BitmapFactory.decodeResource(
context.getResources(), R.mipmap.ic_launcher))
.setSmallIcon(smallIcon);
return notificationBuilder.build();
}
I have made a chat app in firebase and users are being notified when a new message comes by the below codes.
private void sendNotification(String title, String message, String receiver, String receiverUid, String firebaseToken) {
Intent intent = new Intent(this, ChatActivity.class);
intent.putExtra("name",title);
intent.putExtra(Const.ARG_RECEIVER, receiver);
intent.putExtra(Const.ARG_RECEIVER_UID, receiverUid);
intent.putExtra(Const.ARG_FIREBASE_TOKEN, firebaseToken);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
char[] ch = firebaseToken.substring(0,3).toCharArray();
String a = "";
for (char aCh : ch) {
a = a + Integer.toHexString(aCh);
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, Integer.parseInt(a), intent,
PendingIntent.FLAG_ONE_SHOT);
// Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_noti)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
// .setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Integer.parseInt(a), notificationBuilder.build());
}
I want to receive different notification when a different user sends a message but for the same user, the notification should be updated. I tried to set unique id by taking the Unicode of first 3 letters of Firebase token of the user but then also it is showing only one notification. If I chat with 2 people the user with whom I have started chat first shows notification but not the second one.
Please have a look and help. Thanks
Just change FLAG_ONE_SHOT to FLAG_UPDATE_CURRENT.
FLAG_ONE_SHOT :
Flag indicating that this PendingIntent can be used only once.
FLAG_UPDATE_CURRENT :
Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent.
I know this question seems duplicate, but for my requirement, I have searched many posts online, but nothing worked for me.
My requirement
I'm using the Firebase to get the push notifications. When app was opened means everything working fine but my problem is, app is in background/closed if any push notification came means i want to open the particular activity or fragment when clicking on that notification, but it always opening the launcher/main activity.
For this, I have tried the following scenarios
Scenario 1
Used the bundle to transfer notification data from FirebaseMessagingService to the launcher/Main activity and based on the bundle data i'm redirecting to the particular activity/fragment
Scenario 2
By using Sharedpreference
Scenario 3
By using intent.putExtra
following the above scenarios everything working fine when app was opened but if my app was closed means it always redirecting to the main/launcher activity
My code
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("reqTo", messageBody);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
So, does anyone know how to open the particular activity/Fragment when clicking on the Firebase push notification when the app is closed.
public void showNotificationMessage(final String title, final String message, Intent intent) {
// Check for empty push message
if (TextUtils.isEmpty(message))
return;
// notification icon
final int icon = R.drawable.logo;
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
final PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mContext,
0,
intent,
PendingIntent.FLAG_CANCEL_CURRENT
);
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext);
final Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + mContext.getPackageName() + "/raw/notification");
showSmallNotification(mBuilder, icon, title, message, resultPendingIntent, alarmSound);
playNotificationSound();
}
private void showSmallNotification(NotificationCompat.Builder mBuilder, int icon, String title, String message, PendingIntent resultPendingIntent, Uri alarmSound) {
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.addLine(message);
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound)
.setStyle(inboxStyle)
.setSmallIcon(R.drawable.logo)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Config.NOTIFICATION_ID, notification);
}
private void showBigNotification(Bitmap bitmap, NotificationCompat.Builder mBuilder, int icon, String title, String message, PendingIntent resultPendingIntent, Uri alarmSound) {
NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
bigPictureStyle.setBigContentTitle(title);
bigPictureStyle.setSummaryText(Html.fromHtml(message).toString());
bigPictureStyle.bigPicture(bitmap);
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound)
.setStyle(bigPictureStyle)
.setSmallIcon(R.drawable.logo)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Config.NOTIFICATION_ID_BIG_IMAGE, notification);
}
You can check app closed or not like this
if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
This is very similar to my answer given here: https://stackoverflow.com/a/41441631/1291714
In summary though, you need to use Firebase Cloud Messaging and not Firebase Notifications in order to receive a message in the background and do custom processing. You need to send "data" messages to the client and then in your service, you will then always receive the callback, whether the app is in the foreground or background.
With Firebase Notifications, you will only receive the callback when the app is in the Foreground. When the app is in the background, the system will handle and display the notification for a user. You won't be able to customise this notification to open up a different intent.
Read more here: https://firebase.google.com/docs/notifications/android/console-audience
I am using GCM Notification in my app.I parse the data in "onMessageReceived" method where i get some unique "URL" corresponding to GCM Message.When I receive single notification Every thing is fine,But when i receive multiple notifications problem occurs when i tap any notification last received notification url loads.
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
String country = data.getString("country");
String url = data.getString("url");
String description = data.getString("description");
String id = data.getString("id");
}
I get url from GCM Notification
Intent intent = new Intent(this, Detailview.class).putExtra("url", url);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(Mainscreen.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent= stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_ONE_SHOT );
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
contentView.setImageViewResource(R.id.image, R.drawable.ic_stat_ic_notification);
contentView.setTextViewText(R.id.title, message);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setAutoCancel(true)
.setContent(contentView)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Integer.parseInt(id)
/*ID of notification */
, notificationBuilder.build());
so when i tap the notification i am able to launch only last url How i can solve this problem?
use
PendingIntent.FLAG_UPDATE_CURRENT
when creating PendingIntent.
Also before passing intent to PendingIntent set its action different everytime. e.g.:-
intent.setAction(Long.toString(System.currentTimeMillis()));
Just add time of arrive as a differntiator to each notification
PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent,
PendingIntent.FLAG_UPDATE_CURRENT);