I am sending push notification from google firebase for my android app with target of Android 5.0:
My push notification code is:
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String badge = "0";
Uri uri = Uri.parse(
getString(R.string.app_host_name)
);
Map<String, String> data = remoteMessage.getData();
if (data.size() > 0) {
try {
uri = Uri.parse(
data.get("link")
);
badge = data.get("badge");
} catch (NullPointerException e) {
//
}
}
if (remoteMessage.getNotification() != null) {
RemoteMessage.Notification notification = remoteMessage.getNotification();
sendNotification(notification.getTitle(), notification.getBody(), uri.toString(), badge);
}
}
private void sendNotification(String title, String body, String url, String badge) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
if (Patterns.WEB_URL.matcher(url).matches()) {
intent.putExtra("link", url);
}
PendingIntent pendingIntent = PendingIntent.getActivity(
this,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
Resources resources = getApplicationContext().getResources();
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, "default")
.setColor(
resources.getColor(R.color.colorPrimaryDark)
)
.setSmallIcon(
R.drawable.ic_stat_icon
)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setNumber(Integer.parseInt(badge))
.setLargeIcon(
BitmapFactory.decodeResource(
resources,
R.mipmap.ic_launcher
)
)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= 26) {
NotificationChannel notificationChannel = new NotificationChannel(
"default",
"Main notification channel",
NotificationManager.IMPORTANCE_HIGH
);
notificationManager.createNotificationChannel(
notificationChannel
);
}
notificationManager.notify(
1,
notificationBuilder.build()
);
}
And everything is super perfect when application is active/opened/not in background, but when it is not, notifications are not grouped, there is no number displayed, and no reaction on all of this settings at all, what i was able to change is only small icon and circle color via manifest settings
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/ic_stat_icon" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="#color/colorPrimaryDark" />
but why? It's like when app is in background notifications are not using settings from Activity code but using only some kind of "default" one from AndroidManifest.
As you said in the comments:
when app is on background, app isn't taking setNumber, setAutoCancel, setSmallIcon, setLargeIcon options
It is because you are using notification payload to send the notification which only triggers on the foreground.
So when your app is in the background it does not enter this method.
to solve this you can use data payload alone:
"data": {
"titles": "New Title",
"bodys": "body here"
}
since the data payload will enter the onMessageReceived() when your app is in foreground/background.
then in fcm you can do this:
if (remoteMessage.getData().size() > 0) {
title = remoteMessage.getData().get("titles");
body = remoteMessage.getData().get("bodys");
}
Related
I have been trying showing heads up notification as part of Full Screen Intent. It works well in some devices and if device is not locked, it shows up as heads up notification.
But in samsung devices, i have set system notification setting to brief (that shows notification for brief amount of time and then disappear back to system tray). This setting causing heads up notification to appear for small amount of time.
For the same setting, whatsapp and system caller app able to show heads up notification for incoming call.
i have used the following code
Channel creation
private void createFullIntentChannel(){
NotificationManager manager = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + ctx.getPackageName() + "/raw/new_ride.mp3" ) ;
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
String NOTIFICATION_CHANNEL_ID = "com.myapp.app.fullintent";
String channelName = "FullRide";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_HIGH);
chan.setLightColor(Color.BLUE);
chan.enableVibration(true);
chan.setImportance(NotificationManager.IMPORTANCE_HIGH);
chan.setSound(sound, attributes);
chan.setShowBadge(true);
chan.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
manager.createNotificationChannel(chan);
}
}
Setting content and action buttons on notification
public void showFullScreenIntent(final int notifId, String pickLoc, String dropLoc, String tripType, String accountID, String accountType,
String qrMode, Integer stopCount, Integer vhclType, String estInfo) {
setMediaPlayerFile();
if (mediaPlayer != null) {
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
PlayFile();
}
});
PlayFile();
}
// set content pending intent
Intent fullScreenIntent = new Intent(ctx, NewRideRequest.class);
fullScreenIntent.putExtra("Action", "UserChoice");
fullScreenIntent.putExtra("ID", notifId);
fullScreenIntent.putExtra("BookingID", String.valueOf(notifId));
PendingIntent contentIntent = PendingIntent.getActivity(ctx, ACTION_USER_REQUEST_ID,
fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
// set action button accept pending intent
Intent acceptIntent = new Intent(ctx, NewRideRequest.class);
acceptIntent.putExtra("Action", "Accept");
acceptIntent.putExtra("ID", notifId);
acceptIntent.putExtra("BookingID", String.valueOf(notifId));
PendingIntent pendingAcceptIntent = PendingIntent
.getActivity(ctx, ACTION_INTENT_REQUEST_ID, acceptIntent,PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
// set action button reject pending intent
Intent rejectIntent = new Intent(ctx, NotificationAction.class);
rejectIntent.putExtra("Action", "Reject");
rejectIntent.putExtra("ID", notifId);
rejectIntent.putExtra("BookingID", String.valueOf(notifId));
PendingIntent pendingRejectIntent = PendingIntent
.getBroadcast(ctx, ACTION_INTENT_REQUEST_ID, rejectIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + ctx.getPackageName() + "/raw/new_ride.mp3") ;
createFullIntentChannel();
String val = dbHelper.GetVehicleMaster(dbHelper.getReadableDatabase(), vhclType);
// set notification builder
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(ctx, "com.myapp.app.fullintent")
.setSmallIcon(R.drawable.ic_yego_logo)
.setContentTitle("New " + (val.contains(",") ? val.split(",")[0] : "") + " Ride Request")
.setContentText(getContent(stopCount, pickLoc, dropLoc))
//.setSound(sound)
.setOngoing(true)
.setTimeoutAfter(10000)
.setDefaults(Notification.DEFAULT_VIBRATE| Notification.DEFAULT_SOUND)
.addAction(R.drawable.payment_success_1, getActionText(R.string.txt_accept, R.color.colorGreen), pendingAcceptIntent)
.addAction(R.drawable.payment_failed_1, getActionText(R.string.txt_reject, R.color.colorRed), pendingRejectIntent)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setFullScreenIntent(contentIntent, true);
Notification incomingCallNotification = notificationBuilder.build();
incomingCallNotification.sound = sound;
//incomingCallNotification.flags = Notification.FLAG_INSISTENT;
final NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notifId, incomingCallNotification);
Handler handler = new Handler(Looper.getMainLooper());
long delayInMilliseconds = 10000;
handler.postDelayed(new Runnable() {
public void run() {
try {
Log.e("MyApp", "Handler runs");
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
}
notificationManager.cancel(notifId);
}catch (Exception e){
}
}
}, delayInMilliseconds);
}
Please suggest how can i set ongoing heads up notification to be on screen regardless of system setting of notification.
I'm using push notification in my app ,i need to change the notification icon in my home activity as soon as the notification arrives.can we use interface inside firebase service for this purpose.
you can use custom receiver firebase Clude message
in your manifast
<service
android:name=".ReciverClass.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
And Your Reciver
public class MyFirebaseMessagingService extends FirebaseMessagingService {
/**
* Called when message is received.
*
* #param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
// TODO(developer): Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
RemoteMessage.Notification notification = remoteMessage.getNotification();
Map<String, String> data = remoteMessage.getData();
sendNotification(notification, data);
}
private void sendNotification(RemoteMessage.Notification notification, Map<String, String> data) {
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "channel_id")
.setContentTitle(notification.getTitle())
.setContentText(notification.getBody())
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent)
.setContentInfo(notification.getTitle())
.setLargeIcon(icon)
.setColor(Color.RED)
.setLights(Color.RED, 1000, 300)
.setDefaults(Notification.DEFAULT_VIBRATE)
.setSmallIcon(R.mipmap.ic_launcher);
try {
String picture_url = data.get("picture_url");
if (picture_url != null && !"".equals(picture_url)) {
URL url = new URL(picture_url);
Bitmap bigPicture = BitmapFactory.decodeStream(url.openConnection().getInputStream());
notificationBuilder.setStyle(
new NotificationCompat.BigPictureStyle().bigPicture(bigPicture).setSummaryText(notification.getBody())
);
}
} catch (IOException e) {
e.printStackTrace();
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Notification Channel is required for Android O and above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
"channel_id", "channel_name", NotificationManager.IMPORTANCE_DEFAULT
);
channel.setDescription("channel description");
channel.setShowBadge(true);
channel.canShowBadge();
channel.enableLights(true);
channel.setLightColor(Color.RED);
channel.enableVibration(true);
channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500});
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0, notificationBuilder.build());
}}
good luck
You can use BroadcastReceiver for this.
send broadcast when you receive new notification in onMessageReceived method of firebase messaging service:
Intent intent = new Intent();
intent.setAction("com.package.notification");
sendBroadcast(intent);
Then create broadcast receiver in HomeActivity:
BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//your code to change icon
}
};
and register receiver in onCreate and unRegister in onDestroy method of HomeActivity:
IntentFilter filter = new IntentFilter();
filter.addAction("com.package.notification");
registerReceiver(mHandleMessageReceiver, filter);
try {
if (mHandleMessageReceiver != null) {
unregisterReceiver(mHandleMessageReceiver);
}
} catch (Exception e) {
e.printStackTrace();
}
I've created a simple notification and styled it with different colors and general style using a completely new layer created and designed..
Once I trigger the notification though Firebase Messages, when the app is at the foreground, it works perfectly showing the exact styling I need.
But when the app is in the background, it uses it's fugly default style.
Anyway to fix that? Thanks.
The notification java class file code -
public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
private static final String TAG = "FirebaseMessagingServic";
public MyFirebaseMessagingService() {
}
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String title = remoteMessage.getNotification().getTitle();
String message = remoteMessage.getNotification().getBody();
Log.d(TAG, "onMessageReceived: Message Received! \n " +
"Title : " + title + "\n" +
"Message : " + message);
sendNotification(title, message);
}
#Override
public void onDeletedMessages() {
}
private void sendNotification(String title, String messageBody) {
final RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_layout);
remoteViews.setTextViewText(R.id.remoteview_notification_short_message, messageBody);
remoteViews.setTextViewText(R.id.notificationDate, title);
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);
String channelId = "0";
Uri defaultSoundUri= Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/notice");
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.minilogored)
.setContentIntent(pendingIntent)
.setContent(remoteViews)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setLights(0xf14d39, 1500, 2000)
.setPriority(NotificationCompat.PRIORITY_HIGH);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
Please refer the link to know more about Data message and Display message how to handle in onMessageReceived() callback function.
How to handle notification when app in background in Firebase
https://firebase.google.com/docs/cloud-messaging/android/receive
I implemented a notification service in my android app (FirebaseMessagingService). When I click a notification, and the app is closed, the wrong activity is opened. If the notification arrives while the app is open (foreground, background), the activity that is opened is correct.
In the following code, the splash activity (first activity of the project) is opened instead of IntroNoti.class.
This is the code of my FireBaseMessagingService class
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO: Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated.
//Log.d(TAG, "From: " + remoteMessage.getFrom());
//Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
Globals.messageIn = true;
sendNotification(remoteMessage);
}
private void sendNotification(RemoteMessage remoteMessage) {
//Intent intent = new Intent(this, Login.class);
Intent intent = new Intent(this, IntroNoti.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
int icon = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? R.drawable.ic_launcher: R.mipmap.ic_launcher;
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(icon)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setContentText("CIAO CIAO")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
Thanks to the advice of #Eugen Pechanec I added this instruction in my MainActivity. The "messageid" property is only valued when the app is opened by the notification.
Thank you all!
if (getIntent() != null && getIntent().getExtras() != null && getIntent().getExtras().size() > 0) {
Log.d(TAG, "Received extras in onCreate()");
Bundle extras = getIntent().getExtras();
if (!extras.getString("messageid","").isEmpty()) {
Globals.fromNotification = true;
}
}
Use PendingIntent.FLAG_UPDATE_CURRENT flag with pending intent . It's work for me.
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplication(), CHANNEL_ID)
.setSmallIcon(R.drawable.logo)
.setContentTitle(title)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setContentIntent(getPendingIntent())
.setAutoCancel(true);
private PendingIntent getPendingIntent() {
Intent intent = new Intent(getApplication(), ConversationActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
return PendingIntent.getActivity(getApplication(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
I need help. As I wrote in title , I don't know how to keep data from notification when it arrives and the application is in background or killed. What i need to pass ( code below) is a Plate that i need for an elaboration . Here is the code:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String text= remoteMessage.getNotification().getBody();
SharedPreferences s = getSharedPreferences(Constants.PREFS_NAME, Context.MODE_PRIVATE);
Functions.putStringInPrefs(s, Constants.PREFS_ALERT_PLATE, text);
if (Functions.isUserSignedUp(this)) {
if (remoteMessage.getNotification() != null) {
// Log.d(TAG, "Message Title:" + remoteMessage.getNotification().getTitle());
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
try {
sendNotification(remoteMessage.getNotification());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
private void sendNotification(RemoteMessage.Notification notification) {
Intent intent = new Intent(this, FragmentChangeActivity.class);
String prova = notification.getBody();
intent.putExtra("alert_plate" , prova);
GlobalData.alertPlate=prova; // object used as cache
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if(notification.getTitle()!= null){
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.details_icon).setContentTitle(notification.getTitle());
} else{
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.details_icon).setContentTitle(getString(R.string.app_name));
}
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.details_icon).setContentText(notification.getBody())
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(new Random().nextInt((100000000 - 1) + 1) + 1, new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.details_icon).build());
}
}
What do I need to change ? Thank you !
Ps. In manifest the class is declared in the correct way .
There are two types of FCM messages
1) Notification message
2) Data message
So if you want to receive data when the app is close you have to send your data using data message. Your data format will be something look like below
{
"to": "device_id",
"data": {
"param_1": "vale 1",
"param_2": "value 2",
"param_3": "Value 3"
}
}
After receiving the message now you can store your data in shared preference or database whatever you want.