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.
Related
i have created a cloud function which sends data with the notifications. Now i want to open always the specific activity. Unfortunately its opening always the MainActivity. Here is my Code of my Firebase Service:
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> dataSet = remoteMessage.getData();
if (!dataSet.get("kind").equals("0")) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.baseline_send_24)
.setContentTitle(remoteMessage.getData().get("title"))
.setContentText(remoteMessage.getData().get("body"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}else{
Intent intent = new Intent(this, SettingsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.baseline_send_24)
.setContentTitle(remoteMessage.getData().get("title"))
.setContentText(remoteMessage.getData().get("body"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
I dont know if i have to define it somewhere else. Thanks
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction("ACTION.HOME_ACTION");
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
and set this pendingintent in notification
.setContentIntent(pendingIntent)
Incase you are using Firebase for your push notifications. If you use the 'notification' type payload, while sending from server, Firebase manages the display of notification for your app without giving a callback and by default will always open the launcher activity of your app.
You can send extra data from the server side, access that data in your launcher activity getIntent methods and redirect to some other activity as needed
I am creating a Chat Application with Firebase. I send Notifications with Firebase Cloud Functions and recieve them on my phone. But for every message I get a new notification. And now I want to know if it is possible to create a Notification like in WhatsApp, with multible lines for each message in one Notification. My Code for a Notification:
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String notification_tag = remoteMessage.getData().get("senderUiD");
Intent intent = new Intent(getApplicationContext(), Chat_Room.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(remoteMessage.getData().get("username"))
.setContentText(remoteMessage.getNotification().getBody())
.setVibrate(new long[]{1500, 0, 1500, 0})
.setLights(Color.BLUE, 2000, 1000)
.setWhen(remoteMessage.getSentTime())
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setStyle(new NotificationCompat.BigTextStyle().bigText(remoteMessage.getNotification().getBody()))
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent);
notificationManager.notify(notification_tag, 0, notificationBuilder.build());
Thanks for your help ;)
When You Are Receiving Notification Message Your Notification In override last Notification
generate Rendam number
Random ran = new Random();
int notification_tag = r`an.nextInt(6) + 5;`
notificationManager.notify(notification_tag, 0, notificationBuilder.build());
also, fallow few step for grouping notification
NotificationCompat.Builder:contains the UI specification and action information
NotificationCompat.Builder.build() :used to create notification (Which returns Notification object)
Notification.InboxStyle: used to group the notifications belongs to same ID
NotificationManager.notify():to issue the notification.
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());
}
I need display notifications when my server return specific values. I have a service running on android and taskschedule running every time sending request to server, when the server return positive value i need display message on celular display, similar receive message of whatsapp (display icon and notification on display). Anyone have a sample?
I trying this:
PendingIntent resultPendingIntent = PendingIntent.getActivity(
this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
But my application is running as a service.
Please use Using Service to run background and create notification
private void processStartNotification() {
// Do something. For example, fetch fresh data from backend to create a rich notification?
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("Scheduled Notification")
.setAutoCancel(true)
.setColor(getResources().getColor(R.color.colorAccent))
.setContentText("This notification has been triggered by Notification Service")
.setSmallIcon(R.drawable.notification_icon);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
NOTIFICATION_ID,
new Intent(this, NotificationActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setDeleteIntent(NotificationEventReceiver.getDeleteIntent(this));
final NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, builder.build());
}
I am trying to create notifications correctly, but as I can not perform an action when you click on the notification. I hope you can help me or give some clue.
It is recommended to use Notification.Builder and NotificationCompat.Builder from the support library for building notifications.
Also why do use reuse the Intent which was caught by your BroadcastReceiver? It should refer to the actual activity which you want to launch by click.
For example:
Intent intent = new Intent(context, MainActivity.class);
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setContentIntent(PendingIntent.getActivity(context, 0, intent, 0))
.setAutoCancel(true)
.build();
If you doesn't really want to launch some activity, you may start a Service or send a broadcast message instead. See PendingIntent.
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notif = new Notification(R.drawable.ic_launcher, "Text",
System.currentTimeMillis());
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("somekey", "someextra");
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
notif.setLatestEventInfo(this, "Title", "Message", pIntent);
notif.flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(1, notif);
Use Notification.Builder for new version api