Android notifications in another language - android

In my android app I am using Firebase messaging via topics. I am ocassionally sending notifications to my application via Postman to the users which are in that topic:
"notification" : {
"title" : "some title in local lang",
"body" : "some text in local lang",
"sound" : "default"
},
"data": {
"title": "some title in local lang",
"message": "some msg in local lang",
"sound" : "default"
}
I just want to show at least the notification title in english language if the user has switched the language in my app to english.
This is the MyFirebaseMessagingService class, where I tried to work with the notification text, but it doesn't work, it always shows the notification in the sent language, not the app language:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
/**
* Called when message is received.
*
* #param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
// [START receive_message]
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String title = "";
if (Objects.requireNonNull(remoteMessage.getNotification()).getTitle() != null){
title = remoteMessage.getNotification().getTitle();
}
String message = "";
if (remoteMessage.getNotification().getBody() != null){
message = remoteMessage.getNotification().getBody();
}
String lng = readLang();
if (lng.equals("en")) {title="New object added";}
if (readState()) {
sendNotification(title, message);
} }
private void sendNotification(String title, String body) {
Intent i = new Intent(this, Novinky.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pi = PendingIntent.getActivity(this,
0 /* Request code */,
i,
PendingIntent.FLAG_ONE_SHOT);
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
String lng = readLang();
if (lng.equals("en")) {title="New object added";}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,
getString(R.string.default_notification_channel_id))
.setSmallIcon(R.mipmap.ic_notify)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(sound)
.setContentIntent(pi);
NotificationManager manager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.notify(0, builder.build());
}
private boolean readState() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
return sharedPreferences.getBoolean("State", true);
}
private String readLang() {
SharedPreferences sharedPreferences = androidx.preference.PreferenceManager.getDefaultSharedPreferences(this);
return sharedPreferences.getString("hrady_langx", "sk");
}
}
So in both cases (sendNotification and onMessageReceived) I check if (lng.equals("en")) and then define an english title, but this works not...

I found this solution, to use title_loc_key instead of title in Postman data.
Resources/values/strings.xml (default file)
<string name="notification_title_string">Local title</string>
Resources/values-en/strings.xml (English strings file)
<string name="notification_title_string">English title</string>
On the payload instead of using title and message / body keys, we will use title_loc_key to represent the key for our localized title and body_loc_key to represent the key for our localized message.
When sending the payload would look like this:
{
"data": {
"title_loc_key": "notification_title_string",
"body_loc_key": "notification_message_string"
},
"priority": "high",
"condition": "'test' in topics"
}
I will test it when I send the next notification.

Ok, finally I found out - I am now sending only DATA payload, created a notification channel and then I can handle title and message inside onMessageReceived. Now this is working as I wanted.

Related

Android - Notification received but no notification popup (Foreground)

My app is receiving the notification correctly but is failing to show a notification pop up with the received info (If the app is opened).
N.B.: In case if the app is in the background, the notifications are displayed without any issue.
My Code:
I receive the notification in this method:
#Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
Log.d(TAG, "From: " + remoteMessage.getFrom());
if(remoteMessage!=null)
{
String id = null;
String title = null;
String body = null;
String launchPage = null;
if(remoteMessage.getNotification()!=null)
{
title = remoteMessage.getNotification().getTitle();
body = remoteMessage.getNotification().getBody();
}
if(remoteMessage.getMessageId()!=null)
{
id = remoteMessage.getMessageId();
}
Log.i(TAG, "id: " + id);
Log.i(TAG, "title: "+ title);
Log.i(TAG, "body: " + body);
int notif_id = 0;
sendNotification(notif_id, title, body, launchPage);
}
}
then it calls this one (which is supposed to show the notification as i understand):
private void sendNotification(int id, String title, String messageBody, String launchPage)
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(android.R.drawable.stat_sys_download)
.setContentTitle("OMR - "+title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setColor(ContextCompat.getColor(this, R.color.colorPrimary))
.setChannelId(CHANNEL_ID);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannel(notificationManager, CHANNEL_ID);
}
Toast.makeText(this, "Received a notif...", Toast.LENGTH_SHORT).show();
//this line is not showing a notification
notificationManager.notify(id, notificationBuilder.build());
}
Firebase notification
{
"data": {
"message": "data payload only used to force using OnMessageReceived() if in BACKGROUND",
"notif_id": 1
},
"to" : "e04OAVaRw30:APA91bGyv5_tt4IWRkurjlqkqNlCxTBV8oRne18tQ5puniHPItOMgg11kdt56t5jfZnasb4Ms-tH9xUgWQhHy2eM487llRtlM9_V_PoWJI9KSr6XgCaysiDyS",
"notification": {
"title": "Notification",
"body": "This is Notification 2",
"sound":"default"
}
}
Result
the notification is built correctly
sound played correctly
notif put in system tray
BUT no notification popup appears (i have to show a
custom dialog for that)
My Problem lies in this specific line
notificationManager.notify(id, notificationBuilder.build());
which is failing to show the notification
Update
i have read more about Android notifications in
https://developer.android.com/guide/topics/ui/notifiers/notifications
and found out that notifications only show in the notification drawer without popping up (as Heads-up notifications).
and according to this question, i can force the notifications to show as Heads-up notifications if i added a high priority to them. Unfortunately this is not working either.
This image explains it all if you are using FCM.
Well, in my case some of the data I'm passing into the message body is null in the onReceived.
Fixed that and my notification popup was showing again
Change
val notificationManager = packageContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
to
val notificationManager = NotificationManagerCompat.from(packageContext.applicationContext)
helps to me

Xamarin Android Notification Only Working Once

In summary I'm using Firebase Messaging Service and I'm trying to get my messages to show when the app is closed/killed. The weird problem is this sort of works. If I close the application and press send in the Azure Messaging Portal I'll get a notification as you should, however, that is the only notification I'll receive until I open the app and close it again and then I can receive one more. If the app is open I can spam send and I'll get all of the Notifications.
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
[IntentFilter(new string[] { "com.google.android.c2dm.intent.RECEIVE" },
Categories = new string[] { "PACKAGEIDENTIFIER" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
const string TAG = "MyFirstBaseMessagingService";
private int Notification_ID = 2;
public override void OnMessageReceived(RemoteMessage message)
{
Log.Debug(TAG, "From: " + message.From);
if (message.GetNotification() != null)
{
Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body);
SendNotification(message.Data);
}
else
{
SendNotification(message.Data);
}
}
void SendNotification(IDictionary<string,string> message)
{
Drawable drawable = ContextCompat.GetDrawable(this, Resource.Drawable.lsbu);
Bitmap bitmap = ((BitmapDrawable)drawable).Bitmap;
string title = message["title"];
string body = message["body"];
NotificationManager notificationManager = GetSystemService(NotificationService) as NotificationManager;
string ChannelID = GetString(Resource.String.LocalChannelID);
string AppName = GetString(Resource.String.AppName);
NotificationImportance Importance = NotificationImportance.Default;
NotificationChannel mChannel = new NotificationChannel(ChannelID, AppName, Importance);
notificationManager.CreateNotificationChannel(mChannel);
Notification.Builder builder = new Notification.Builder(this, ChannelID)
.SetWhen(DateTime.Now.Second)
.SetSmallIcon(Resource.Drawable.miniwozzad)
.SetLargeIcon(bitmap)
.SetContentTitle(title)
.SetContentText(body)
.SetChannelId(ChannelID);
var notification = builder.Build();
notificationManager.Notify(Notification_ID, notification);
Notification_ID++;
}
}
This being my payload in the Azure Test Portal
{"data":{"body": "Another big message", "test": 1, "title": "A Big Title2" }}
This is my FirebaseMessagingService class, I'm not sure what I'm missing here. Any help would be appreciated!

Error when push notification by postman but not when send by Firebase console [duplicate]

I'm building an Android app which will receive push notifications. I've got Firebase Cloud Messaging setup and pretty much working, such that I can send the following payload to a valid token and the notification and data are received.
Using url https://fcm.googleapis.com/fcm/send
{
"to":"<valid-token>",
"notification":{"body":"BODY TEXT","title":"TITLE TEXT","sound":"default"},
"data":{"message":"This is some data"}
}
My app receives it correctly and can deal with it.
The only slight wrinkle is that I get the following exception thrown in the debug:
Error while parsing timestamp in GCM event
java.lang.NumberFormatException: Invalid int: "null"
at java.lang.Integer.invalidInt(Integer.java:138)
...
It doesn't crash the app, it just looks untidy.
I've tried adding a timestamp item to the main payload, the notification, the data, and also tried variations such as time but can't seem to get rid of the exception (and google as I might, I can't find an answer).
How do I pass the timestamp so it stops complaining?
Edited: Here is my onMessageReceived method, but I think the exception is thrown before it gets here
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
//TODO Handle the data
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
}
Thanks in advance,
Chris
Even though notification is apparently a supported element (according to the Firebase web docs), the only way I could get rid of the exception was to remove it entirely, and use the data section only, and then in my app create a notification (rather than letting firebase do the notification).
I used this site to work out how to raise the notifications: https://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/
My notification now looks like the following:
$fields = array("to" => "<valid-token>",
"data" => array("data"=>
array(
"message"=>"This is some data",
"title"=>"This is the title",
"is_background"=>false,
"payload"=>array("my-data-item"=>"my-data-value"),
"timestamp"=>date('Y-m-d G:i:s')
)
)
);
...
<curl stuff here>
...
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
My onMessageReceived looks like this:
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
try {
JSONObject json = new JSONObject(remoteMessage.getData().toString());
handleDataMessage(json);
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
}
which calls handleDataMessage which looks like this:
private void handleDataMessage(JSONObject json) {
Log.e(TAG, "push json: " + json.toString());
try {
JSONObject data = json.getJSONObject("data");
String title = data.getString("title");
String message = data.getString("message");
boolean isBackground = data.getBoolean("is_background");
String timestamp = data.getString("timestamp");
JSONObject payload = data.getJSONObject("payload");
// play notification sound
NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
notificationUtils.playNotificationSound();
if (!NotificationUtils.isBackgroundRunning(getApplicationContext())) {
// app is in foreground, broadcast the push message
Intent pushNotification = new Intent(ntcAppManager.PUSH_NOTIFICATION);
pushNotification.putExtra("message", message);
LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
} else {
// app is in background, show the notification in notification tray
Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
resultIntent.putExtra("message", message);
showNotificationMessage(getApplicationContext(), title, message, timestamp, resultIntent);
}
} catch (JSONException e) {
Log.e(TAG, "Json Exception: " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
this then calls showNotificationMessage
/**
* Showing notification with text only
*/
private void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent) {
notificationUtils = new NotificationUtils(context);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
notificationUtils.showNotificationMessage(title, message, timeStamp, intent);
}
And subsequently notificationUtils.showNotificationMessage
public void showNotificationMessage(String title, String message, String timeStamp, Intent intent) {
showNotificationMessage(title, message, timeStamp, intent, null);
}
public void showNotificationMessage(final String title, final String message, final String timeStamp, Intent intent, String imageUrl) {
// Check for empty push message
if (TextUtils.isEmpty(message))
return;
// notification icon
final int icon = R.mipmap.ic_launcher;
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, timeStamp, resultPendingIntent, alarmSound);
playNotificationSound();
}
private void showSmallNotification(NotificationCompat.Builder mBuilder, int icon, String title, String message, String timeStamp, 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)
.setWhen(getTimeMilliSec(timeStamp))
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(ntcAppManager.NOTIFICATION_ID, notification);
}
More detail in the link above, and it's a lot of handling but at least the exception's gone and I'm in control of the notifications.
I've updated com.google.firebase:firebase-messaging to 17.3.4 and issue disappeared.
I was running into this same error, I resolved by adding a ttl value to the payload.
{
"to":"<valid-token>",
"notification":{"body":"BODY TEXT","title":"TITLE TEXT","sound":"default"},
"data":{"message":"This is some data"},
"ttl": 3600
}
I had the same problem, I've just set "body" parameter in notification and error dissapeared.
What worked for me:
Upgrading, not only firebase-messaging, but all Firebase libraries to the last version. In android/app/build.gradle:
dependencies {
implementation "com.google.firebase:firebase-core:16.0.0" // upgraded
implementation "com.google.firebase:firebase-analytics:16.0.0" // upgraded
implementation 'com.google.firebase:firebase-messaging:17.3.4' // upgraded
// ...
implementation "com.google.firebase:firebase-invites:16.0.0" // upgraded
// ...
}
Not all of them are at version 17.x
Format below (no notification body, nor are there any arrays) fixed the timestamp exception for me:
{
"to": "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
"data": {
"message": "message",
"title": "hello",
}
}
Tested fine with http://pushtry.com/
The only reason I included the long 'eeee...' is that is the exact size of my token.
replace
"notification" : {
"title" : "title !",
"body" : "body !",
"sound" : "default"
},
"condition" : "'xxx' in topics",
"priority" : "high",
"data" : {
....
by (remove notification) :
{
"condition" : "'xxxx' in topics",
"priority" : "high",
"data" : {
"title" : "title ! ",
"body" : "BODY",
......
}
And on you code :
Replace :
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
remoteMessage.getNotification().getTitle();
remoteMessage.getNotification().getBody();
}
by
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
remoteMessage.getData().get("title");
remoteMessage.getData().get("body");
}
In my case My error was "AndrodManifest.xml"
I miss one service (actually Android studio's firebase assistant is missing my permission. :) )
original
<service android:name=".fcm.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
</application>
solution
<service android:name=".fcm.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service android:name=".fcm.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>

Android - FCM Push Notification not received after click_action is included

I am developing an Android application in which I would like to receive a Push notification when application state is both in Foreground and Background.
It was working fine before the click_action is added but after adding it does not make the push notification received when the application is background or killed. After some research, I could understand that I will not be able to receive the Push in Background if the FCM is "notification" message type but only "data" message type.
As FCM provides click_action attribute by default and also provides the method getClickAction() to get it in onMessageReceived(), Should I really use click_action in data message ?
The bundle in onMessageReceived
Bundle[{
google.sent_time = 1521177008895,
google.ttl = 3600,
gcm.notification.e = 1,
lastName = Test,
profileUrl = ,
roleId = ,
userId = 140128,
gcm.notification.badge = 1,
gcm.notification.sound =
default,
gcm.notification.title = Someone
try to login with your credentials,
roleName = ,
userName = test,
flag = 0,
from = 612005318045,
type = 0,
gcm.notification.sound2 = simpleSound,
firstName = Test,
gcm.notification.notification_id = 1140,
google.message_id = 0: 1521177008900292 % c05b1316c05b1316,
notification = Someone
try to login with your credentials,
gcm.notification.body = Someone
try to login with your credentials,
gcm.notification.icon = myApp,
notificationId = 2047669,
gcm.notification.notification_type = 1,
gcm.notification.click_action = com.my.push.activities.OPEN_NOTIFICATION_LIST,
gcm.notification.notification_message = TEST MESSAGE,
notificationDate = Fri Mar 16 05: 10: 08 GMT 2018,
collapse_key = com.my.push,
gcm.notification.notification_title = APP
}]
The code snippet of the way it is handled in onMessageReceived
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
......
......
showNotification(remoteMessage);
}
public void showNotification(RemoteMessage remoteMessage) {
try {
Map<String, String> response = remoteMessage.getData();
Intent intent = prepareIntent(remoteMessage);
PendingIntent pIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "D4E_ANDROID")
.setContentTitle("New Notification")
.setContentText(response.get("notification"))
.setSmallIcon(R.drawable.d4e_logo)
.setContentIntent(pIntent)
.setAutoCancel(true)
.addAction(R.drawable.view_icon, "View", pIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(Integer.parseInt(response.get("notificationId")), builder.build());
} catch (Exception exception) {
Log.e("OnREC", exception.toString());
}
}
public Intent prepareIntent(RemoteMessage remoteMessage) {
Map<String, String> response = remoteMessage.getData();
Intent intent;
boolean isAppInBackground;
if (SessionContext.isLoggedIn()) {
isAppInBackground = SessionHelper.initializeSessionHelper().isAppInBackground(this);
Log.e("LOGGGGG", isAppInBackground + "");
if (isAppInBackground) {
intent = new Intent(this, SplashScreen.class);
} else {
intent = new Intent(remoteMessage.getNotification().getClickAction());
}
}
} else {
intent = new Intent(this, LoginActivity.class);
}
return intent;
}
Please anyone guide me to find the solution.
if you can receive FCM notification sent through console, then potentially something is wrong with build the notification. one suspect is that you are not using "icon" field.
If you can debug android device, set breakpoint here and see if you get error or missed some resource.
https://github.com/evollu/react-native-fcm/blob/master/android/src/main/java/com/evollu/react/fcm/SendNotificationTask.java#L46

How to initialize a RemoteMessage object with a Json file?

I am dealing with FCM messages and I have the following function:
public void onMessageReceived(final RemoteMessage remoteMessage)
{
final Map<String, String> data = remoteMessage.getData();
//...
}
For testing purposes, I have a Json file containing data- and I want to send that Json file from my test method to onMessagereceived() . Hence I need to initialize a RemoteMessage object with the Json file and pass this RemoteMessage object to the function. How do I initialize this?
My JSON file:
{
"data": {
"id" : "4422",
"type" : "1",
"imageUrl" : "https://image.freepik.com/free-vector/android-boot-logo_634639.jpg",
"smallTitle" : "DoJMA v2",
"smallSubTitle" : "Update now from Google Play Store",
"ticker" : "New update for DoJMA",
"contentInfo" : "",
"link" : "https://photo2.tinhte.vn/data/avatars/l/1885/1885712.jpg?1402763583",
"className" : "HomeActivity",
"page" : "2",
"bigTitle" : "DoJMA Android app version 2 released!",
"bigSubTitle" : "Hi folks! New DoJMA update is here! Major redesigning and improvements! This app was made by the Mobile App Club.They work really hard man...and get good products",
"bigSummaryText" : "Update now"
},
"registration_ids": ["dQYmpLUACXQ:APA91bGl-NoIMJ2_DcctF5-OA8ghyWuyrMfsz3uhlj1BySl6axkAsmv5y_7YGfpQQJ2E0lP_fTcxpHpZdkJzY1tbcWA36e78ooxC_b0a1PAank9gFIAUHVZkHKmZT70MPZosCgvRlVfq","dfLXnRI36qY:APA91bFyjLblijVIjGLCGWVeB1B0z5j_3TYqRytJ-8hvuUESpDlX59gWF3hU-I-kA4VrRCPpEVFWl18ZarnPjqxxtZgFkVxoLr77HRex27VN7Mh3xupWykmKq_nnVIlVzrODKwKI7ktM"]
}
you can test using Postman For Single User.
Send a notification with a JSON payload
URL: https://fcm.googleapis.com/fcm/send
Headers:
Authorization: key=<your-api-key>
Content-Type: application/json
Body (click on the 'raw' tab):
{
"to": "dQYmpLUACXQ:APA91bGl-NoIMJ2_DcctF5-OA8ghyWuyrMfsz3uhlj1BySl6axkAsmv5y_7YGfpQQJ2E0lP_fTcxpHpZdkJzY1tbcWA36e78ooxC_b0a1PAank9gFIAUHVZkHKmZT70MPZosCgvRlVfq",
"data": {
"id": "4422",
"type": "1",
"imageUrl": "https://image.freepik.com/free-vector/android-boot-logo_634639.jpg",
"smallTitle": "DoJMA v2",
"smallSubTitle": "Update now from Google Play Store",
"ticker": "New update for DoJMA",
"contentInfo": "",
"link": "https://photo2.tinhte.vn/data/avatars/l/1885/1885712.jpg?1402763583",
"className": "HomeActivity",
"page": "2",
"bigTitle": "DoJMA Android app version 2 released!",
"bigSubTitle": "Hi folks! New DoJMA update is here! Major redesigning and improvements! This app was made by the Mobile App Club.They work really hard man...and get good products",
"bigSummaryText": "Update now"
}
}
Source : https://firebase.google.com/docs/cloud-messaging/concept-options
TL;DR: Initializing a RemoteMessage object is not possible.
Attempting to access initialize a RemoteMessage from your own class would return an error:
'RemoteMessage(android.os.Bundle)' is not public in 'com.google.firebase.messaging.RemoteMessage'. Cannot be accessed from outside package.
I presume you're attempting to do this for testing purposes (stubbing?). In general (not gonna say best practice, since I'm not that entirely sure that it is best practice), it is advisable to have a separate method that accepts a specific value that you need. Referring to the official example (onMessageReceived) (removed some stuff):
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
if (/* Check if data needs to be processed by long running job */ true) {
// For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
scheduleJob();
} else {
// Handle message within 10 seconds
handleNow();
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
// 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.
}
/**
* Schedule a job using FirebaseJobDispatcher.
*/
private void scheduleJob() {
// [START dispatch_job]
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
Job myJob = dispatcher.newJobBuilder()
.setService(MyJobService.class)
.setTag("my-job-tag")
.build();
dispatcher.schedule(myJob);
// [END dispatch_job]
}
/**
* Handle time allotted to BroadcastReceivers.
*/
private void handleNow() {
Log.d(TAG, "Short lived task is done.");
}
/**
* Create and show a simple notification containing the received FCM message.
*
* #param messageBody FCM message body received.
*/
private void sendNotification(String messageBody) {
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.ic_stat_ic_notification)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
Focusing on the comment that mentions:
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.
This points out the sendNotification() method that accepts a String parameter. Depending on your case, instead of attempting to pass an initialized RemoteMessage (which sadly isn't possible, or at the very least is not advisable), you could simply pass an object that your method needs.
With that, you pretty much tested the method that handles the message without depending on an object that isn't a class you made in the first place.
PS: Some of my explanations can be confusing, but I hope this made sense.

Categories

Resources