GCM notifications not being recieved when app in foreground - android

I am trying to setup push notifications to my app containing both data and notification payloads as described in the official reference. I want both payloads so that I will get an automatic notification when my app is in the background, and the app can handle things itself while in the foreground.
The onMessageReceived() method in my GcmListenerService is not called when both payloads are included and the app is in the foreground. I have to leave out the notification payload for it to work.
When the app is in the background, it works fine. I get a notification and can open the app when the notification is clicked and do whatever I want from there.
I have looked at this question, but they were trying to get a notification when the app is in foreground. I realize I won't get a notification in that case, I just want to get the data.
Relevant portion of AndroidManifest.xml:
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="arkenterprises.garage_o_matic.gcm" />
</intent-filter>
</receiver>
<service
android:name=".MyGcmListenerService"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
MyGcmListenerService.java:
public class MyGcmListenerService extends GcmListenerService {
private static final String TAG = "MyGcmListenerService";
#Override
public void onMessageReceived(String from, Bundle data) {
Log.d(TAG, "Data: " + data);
String message = data.getString("message");
String doorStatus = data.getString("DoorStatus");
String time = data.getString("time");
Bundle notification = data.getBundle("notification");
Log.d(TAG, "From: " + from);
Log.d(TAG, "Message: " + message);
Log.d(TAG, "DoorStatus: " + doorStatus);
Log.d(TAG, "Time: " + time);
Log.d(TAG, "Notification: " + notification);
}
Server code that sends the message:
nowString = datetime.datetime.now().strftime("%c")
gcmSendURL = 'https://gcm-http.googleapis.com/gcm/send'
gcmHeaders = {'Authorization': 'key=' + API_KEY, 'Content-Type': 'application/json'}
gcmDataPayload = {'message': 'Garage door opened', 'DoorStatus': 'Open', 'time': nowString}
gcmNotificationPayload = {'title': 'Garage Door Has Opened', 'body': 'Garage door opened at {}'.format(nowString), 'icon': 'ic_door_open_notification2'}
print('Garage door opened at {}'.format(nowString))
gcmPayload = {'to': regIDs[0].strip(), 'priority': 'high', 'delay_while_idle': False, 'time_to_live': 86400, 'data': gcmDataPayload}
# gcmPayload = {'to': regIDs[0].strip(), 'priority': 'high', 'delay_while_idle': False, 'time_to_live': 86400, 'notification': gcmNotificationPayload}
# gcmPayload = {'to': regIDs[0].strip(), 'priority': 'high', 'delay_while_idle': False, 'time_to_live': 86400, 'content_available': True, 'data': gcmDataPayload, 'notification': gcmNotificationPayload}
print("\nPayload: {}".format(gcmPayload))
r = requests.post(gcmSendURL, headers=gcmHeaders, json=gcmPayload)
print("\nRequest response: " + r.text)

Yes, you can send messages that have both notification and data payloads.
I found this Stack overflow ticket related to your inquiry, It says that Intent filters will always fire your manifested application as per standard android behavior. Make sure that your logic flow is able to handle the fact that your launch may come from places. Try to implement 'GcmMessageHandler' your application must check saved state and sees if user needs to see screen and automatically show notifications.

I had some strange behaviour with samples, but in final I resolved that by removing implementation of GcmListenerService and adding implementation of GcmReceiver.
public class MyGcmReceiver extends GcmReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
}
};
and manifest
<receiver
android:name=".MyGcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</receiver>

Related

Firebase message, Android app in background not intercept push notification after click on message

I use firebase messaging to send push notification. I have a small problem with my android app. In the iOS version, I can easily pass parameters after clicking on a notification when the app is in background.
In Android version I can intercept messages when the app is in the foreground, but not when the app is in the background (I use library com.google.firebase:firebase-messaging:17.3.4):
my AndroidManifest.xml
<activity android:name=".MainActivity"
android:screenOrientation="portrait"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="OPEN_NOTIFY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
....
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
My notification payload:
notification:{
title: "mytitle",
text: "MYTEXT",
"data":{
"type" : "message",
"id_message" : res
},
"click_action" : "OPEN_NOTIFY"
},to: token
When I click on the notification, it should open the MainActivity and I would like to have the id_message
my MyFirebaseMessagingService.java
#Override
public void onMessageReceived(RemoteMessage remoteMessage){
//never enters if the app is in background
if(remoteMessage.getNotification() != null){
//I READ NOTIFY WHEN APP IS IN FOREGROUND
}
}
I try to get parameters directly from the Inten, but this solution doesn't work
my MainActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
....
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
Object value = getIntent().getExtras().get(key);
Log.d(TAG, "Key: " + key + " Value: " + value);
}
}
....
}
I can't intercept my notify:
output:
Key: google.sent_time Value: 1540026384768
Key: google.ttl Value: 2419200
Key: from Value: 635549396240
Key: google.message_id Value: 0:1540026384773623%6b8fed3d6b8fed3d
Key: collapse_key Value: ....
Am I following the right path? Thank You
According to official Firebase docs, you can't handle notification{} part of notification (you can only set icon of it in manifest), BUT here's solution: send necessary data via data{} part. You can now handle it with MyFirebaseService and show notification if you want and as you want: data will appear in remoteMessage.toIntent().getExtras().
In your case, send notification like this:
// ignore notification {}
data {
"type" : "message",
"id_message" : res
}
to: key
(do not place data into notification block)
I can write working code of sending and receiving notification, if you want

FirebaseMessasingService gets notifications from my server, but if I use the FirebaseConsole, my class is not being called

This is what I have inside my AndroidManifest:
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name="io.smooch.core.service.SmoochService" />
<service android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
I am using version: implementation 'com.google.firebase:firebase-messaging:17.0.0'
This is how my FirebaseMessagingService looks like:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public void onMessageReceived(final RemoteMessage message) {
Log.i("", "MyFirebaseMessagingService onMessageReceived");
final Map data = message.getData();
FcmService.triggerSmoochNotification(data, this);
final Map bundle = data;
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
#Override
public void run() {
Log.i("", "GLOBAL intances before GCM:" + Realm.getGlobalInstanceCount(PSApplicationClass.Config));
try {
String title = getString(R.string.alert_title_warning) + " " + getString(R.string.trips_remaining_free, user.getTrips_left());
String message = getString(R.string.alert_freemium_upgrade);
Notification.generateNotificationStandard(MyFirebaseMessagingService.this, title, message, null, null, false, false);
} catch (Exception e) {
Utils.appendLog("GCMIntentService onMessageReceived error: " + e.getMessage(), "E", Constants.PUSH_NOTIFICATIONS);
}
}
});
}
}
And basically I put a breakpoint at the onMessageReceived of this function.
If the server sends a notification to my app, it will be received by this Service, and I can handle it as needed.
BUT. If I use the FirebaseConsole -> GROW -> Could Messaging -> I create a new Message and I send it. My app will get the notifications. but it will be created "automatically" without entering my onMessageReceived method.
How can I force it to do so?
This is the data that I send via the console:
https://s3.amazonaws.com/uploads.hipchat.com/39260/829560/7Pp0KJIErZ8XVZZ/upload.png
What am I doing wrong?
EDIT:
Like I said, I get notifications, but the notifications are being created automatically instead of going through my FirebaseMessaging Service.
I did manage to change the icon like this:
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/notification_icon" />
But I still need to run other code when I get it, not just show it. and that I do not know how

How to get Firebase Combined Message in Background app

Reading documentation of firebase about cloud message in combined way, I've had problems to get data message in android device.
var payload = {
notification: {
title: "Test",
body: "Test body"
},
data: {
score: "850",
close: "635.67"
}
};
I expect to get data field at 'onMessageReceived' in android device, but I got only a notification message. I tried to tap the message and I still got nothing at 'onMessageReceived'.
I've add a intent filter to trigger the main activity, add 'clickAction'(as referred in firebase reference) and 'click_action'(as I saw in some questions) in notification payload, and I also got the same.
MANIFEST
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#mipmap/ic_launcher" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="#color/blue" />
<service
android:name=".connection.TestFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".connection.TestFirebaseIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
Java Code:
public class FirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "TestFirebase";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
LogNotification.d(TAG, "NOTIFICATION - From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
LogNotification.d(TAG, "NOTIFICATION - Message data payload: " + remoteMessage.getData().toString());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
LogNotification.d(TAG, "NOTIFICATION - Message Notification Body: " + remoteMessage.getData().toString());
}
}
}
Some tips?
If you have the notification key in your payload then the message becomes a notification message.
Notification messages are displayed automatically when the app is in background.
When the notification is clicked FCM launches your main activity (or another one if you specified click_action)
In the activity you can use getIntent() to get the intent that launched the activity.
If your activity was launched by FCM you will find the data payload in the intent used to launch the activity.

How we can implement GCM as well as parse notification in same application?

My app have one third party library, in this library implemented parse notification for notification.
But app required some notification from GCM.
So how can we implement both receiver in same project.
Any one have idea to implement both notification in same application ?
If possible then how ?
Please help me out.
Thank you in advance.
As far as I can see you just need to create a new a Broadcast Receiver to GCM.
Or even add a verification by action in your existing one. For example, in below's code I check if the action coming is a registration push from GCM or a push of mine by checking the intent action.
if (intent != null) {
String action = intent.getAction();
Log.w(TAG, "Registration Receiver called");
if ("com.google.android.c2dm.intent.REGISTRATION".equals(action)) {
Log.w(TAG, "Received registration ID");
final String registrationId = intent.getStringExtra("registration_id");
String error = intent.getStringExtra("error");
Log.d(TAG, "dmControl: registrationId = " + registrationId + ", error = " + error);
// TODO Send this to my application server
} else {
//Your code here
}
Your filter is gonna be something like...
<receiver
android:name="com.myapp.push.ExternalReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.myapp.blablabla" />
</intent-filter>
</receiver>

GCM and Parse notification conflict

I need my android app to work with two push services, GCM and parse.
My problem is that I can't find a way for register correctly to Parse, get parse notification and GCM notification too. I can reach all these things individually, but never together.
My current implementation looks this way:
<!-- GCM BradcastReceiver & Service -->
<service android:name=".GcmIntentService"
android:enabled="true"/>
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<receiver
android:name=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter
android:priority="100"> <!-- higher priority to GCM messages -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.twentyLines.GCM_NOTIFICATION_ACTION" />
<category android:name="com.twentyLines.app" />
</intent-filter>
</receiver>
This is the broadcastReceiver for GCM, and the one below is the other receiver for parse:
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter android:priority="0">
<!--<action android:name="com.google.android.c2dm.intent.RECEIVE" />-->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.twentyLines.app" />
</intent-filter>
</receiver>
<!-- Can remove this if application is already registered for GCM -->
<receiver android:name="com.parse.ParseBroadcastReceiver" android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="com.twentyLines.PARSE_NOTIFICATION_ACTION" />
</intent-filter>
</receiver>
I've tried to add a custom broadcast receiver to handle just parse notification, so I can avoid it to handle GCM too. I've done it like this:
<receiver android:name="com.twentyLines.app.ParseBroadcastReceiver" android:exported="false" >
<intent-filter>
<action android:name="com.twentyLines.PARSE_NOTIFICATION_ACTION" />
</intent-filter>
</receiver>
This is the implementation of my BroadcastReceiver for GCM, that avoid parse notification to be displayed.
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
boolean isFromParse = intent.getExtras().get("action").equals("com.twentyLines.PARSE_NOTIFICATION_ACTION");
if (isFromParse)
return;
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_CANCELED);
}
}
And this is the implementation for my PARSE custom BroadcastReceiver that SHOULD avoid GCM to be showed.
The parse custom receiver is never invoked, I think because the com.parse.GcmBroadcastReceiver handle the notification itself instead of pass them.
The result is that, when I send a notification from my server to GCM receiver, this is retrieved from both, and double notification is showed.
(double is already good, every time I uninstall and reinstall my app Parse register another user.. the "UniqueId" I send to parse every time is not so UNIQUE for them).
What have I tried?
I'm really getting crazy, I've tried about everything.
- I've read all related questions, and no one is good for me;
- I've tried to remove the parse receiver, and this cause a parse exception (so doesn't register to parse)
- I've tried to set the intent to RESULT_CANCELED, but in some way parse get it before GCM, so isn't working when I use GCM.
- I've changed about all using cowboy-coding, and it still not work...
Any help will be really welcome. Thank you guys!
EDIT - ADD WORKING MANIFEST
<!-- GCM BradcastReceiver & Service -->
<service android:name=".GcmIntentService"
android:enabled="true"/>
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<receiver
android:name=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter
android:priority="2"> <!-- higher priority to GCM messages -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.twentyLines.GCM_NOTIFICATION_ACTION" />
<category android:name="com.twentyLines.app" />
</intent-filter>
</receiver>
<!-- Parse service broacastReceiver and receiver. -->
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter android:priority="1">
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.twentyLines.app" />
</intent-filter>
</receiver>
<!-- Can remove this if application is already registered for GCM -->
<receiver android:name="com.parse.ParseBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="com.twentyLines.PARSE_NOTIFICATION_ACTION" />
</intent-filter>
</receiver>
<receiver android:name="com.twentyLines.app.ParseBroadcastReceiver" android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="com.twentyLines.PARSE_NOTIFICATION_ACTION" />
</intent-filter>
</receiver>
EDIT 2: HOW I REGISTER PARSE AND GCM
I register Parse in my application class:
Parse.initialize(this, PARSE_APP_KEY_VALUE, PARSE_CLIENT_KEY_VALUE);
PushService.setDefaultPushCallback(getApplicationContext(), MainActivity.class);
final ParseInstallation installation = ParseInstallation.getCurrentInstallation();
final String androidId = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
installation.put("UniqueId", androidId);
installation.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
Log.d("Parse installation saved in background", "If operation successfull, 'e' have to be NULL e= " + e);
}
});
}
}, 5000
);
And I get gcm registration_id in my MainActivity:
// Check if there is a saved registration_id in shared_prefs,
// or if app version has changed
private String getSavedRegistrationId() {
final SharedPreferences gcmShared = getGCMSharedPrefss();
String registrationId = gcmShared.getString(Constant.GCM_REGISTRATION_ID_KEY, "");
if (registrationId.isEmpty()) {
Log.i("GCM", "Registration not found.");
return "";
}
int registeredVersion = gcmShared.getInt(Constant.APP_VERSION_KEY, Integer.MIN_VALUE);
int currentVersion = getAppVersion(this);
if (registeredVersion != currentVersion) {
clearSavedGCMRegistrationId();
Log.i("GCM", "App version changed.");
return "";
}
return registrationId;
}
// If there isn't, request one
private void registerInBackground() {
new AsyncTask<Void, Void, String>() {
#Override
protected String doInBackground(Void... params) {
String msg;
try {
if (_gcm == null) {
_gcm = GoogleCloudMessaging.getInstance(MainActivity.this);
}
String regid = _gcm.register(Constant.GCM_SENDER_ID);
msg = "Device registered, registration ID=" + regid;
sendRegistrationIdToBackend(regid);
storeRegistrationId(regid); // Save reg_id to shared
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
// If there is an error, don't just keep trying to register.
// Require the startupLoggedUser to click a button again, or perform
// exponential back-off.
}
return msg;
}
#Override
protected void onPostExecute(String msg) {
Log.d("GCM registration", msg);
}
}.execute(null, null, null);
}
We have two broadcast receivers here to listen c2dm intent
.GcmBroadcastReceiver : lets call this as GCM receiver... Parse Push will never come to this receiver.
com.parse.GcmBroadcastReceiver : lets call this as Parse receiver
As you have given higher priority to GCM receiver, broadcast will come to GCM receiver first and then to Parse receiver. This does not guarantee that Broadcast will not go to Parse receiver. You need to add abortBroadcast(); as last line in onReceive method to make sure that Parse receiver is not triggered when we have GCM receiver working.
As per Parse Push notification guidelines, Push is received with a specific intent action. Data is sent to broadcast receiver registered with that action.
In your case, if push is received with action "com.twentyLines.PARSE_NOTIFICATION_ACTION", you can have a custom broadcast receiver to listen to this action. In that broadcast receiver you can fetch data by below code,
try {
String action = intent.getAction();
String channel = intent.getExtras().getString("com.parse.Channel");
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
Log.d(TAG, "got action " + action + " on channel " + channel + " with:");
Iterator itr = json.keys();
while (itr.hasNext()) {
String key = (String) itr.next();
Log.d(TAG, "..." + key + " => " + json.getString(key));
}
} catch (JSONException e) {
Log.d(TAG, "JSONException: " + e.getMessage());
}
When there is GCM push, this custom receiver will never get broadcast event as the C2DM broadcast is being aborted in GCM receiver (".GcmBroadcastReceiver")
Hope This Helps!

Categories

Resources