In my app I want to receive FCM messages even when the app is in foreground. So I've set everything up according to https://firebase.google.com/docs/cloud-messaging/android/receive#sample-receive
Yet while my app is in the foreground, the onMessageReceived method is not called. Note that when my app is in background it gets the notifications, so the connection to FCM itself works fine.
application part of my AndroidManifest.xml
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/ic_stat_ic_notification" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="#color/colorAccent" />
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
MyFirebaseMessagingService.kt
class MyFirebaseMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
Timber.i("onMessageReceived") // never called
}
}
I send my notifications using the FCM console. Any idea what I am doing wrong? Why is onMessageReceived not called while my app is in the foreground?
try replacing your service tag in the manifest file with :
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
in the onMessageReceived() refer here you can get the notification / data payload :
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " +
remoteMessage.getNotification().getBody());
}
}
}
You can refer here for more information.
Related
I'm facing an issue with FirebaseMessaging class.
public class FirebaseService extends FirebaseMessagingService {
private static final String TAG = FirebaseService.class.getSimpleName();
#Override
public void onCreate() {
super.onCreate();
GlobalApp.logDebug(TAG, "Firebase Service started");
}
}
The onCreate is not called.
I don't receive any notifications
I'm using implementation 'com.google.firebase:firebase-messaging:20.2.0'
The google documentation is not clear when that service starts.
This is my manifest :
<service
android:name=".common.google.FirebaseService"
android:enabled="true"
android:exported="false"
android:stopWithTask="false"
android:directBootAware="true">
<intent-filter android:priority="-500">
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
Any idea ?
Thanks
Thanks to #azizbekian
<service
android:name=".common.google.FirebaseService"
android:enabled="true"
android:exported="false"
android:stopWithTask="false"
android:directBootAware="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
I just had to remove the android:priority="-500"...
Facepalm..
I'm trying to receive propertly a message from FireBase when my app is in background but not working anithing. I've seen some Stackoverflow problems like mine but not working anything.
Could anyone help me to find the problem?
I need to do it when my app is in background but not working.
here AndoridManifest.xml
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-feature android:name="android.hardware.type.watch" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#android:style/Theme.DeviceDefault"
tools:targetApi="ice_cream_sandwich">
<service
android:name=".MyfirebaseMessagingService" android:exported="false" >
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".MyFirebaseInstanceIDService" android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<uses-library
android:name="com.google.android.wearable"
android:required="true" />
<!--
Set to true if your app is Standalone, that is, it does not require the handheld
app to run.
-->
<meta-data
android:name="com.google.android.wearable.standalone"
android:value="true" />
<!-- [START fcm_default_channel] -->
<!-- [START fcm_default_channel] -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="#string/default_notification_channel_id" />
<activity
android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<!--<action android:name="android.intent.action.VIEW" />-->
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="gizmos"
android:scheme="example" />
</intent-filter>
</activity>
</application>
MyfirebaseMessagingService.java:
public class MyfirebaseMessagingService extends FirebaseMessagingService {
public static String TAG = "MyFirebaseMsgService";
private LocalBroadcastManager broadcaster;
#Override
public void onCreate() {
super.onCreate();
createNotificationChannel();
broadcaster = LocalBroadcastManager.getInstance(this);
}
#Override
public void onDestroy() {
super.onDestroy();
}
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = getString(R.string.default_notification_channel_id);
CharSequence name = getString(R.string.default_notification_channel_id);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(channelId, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
Intent home = new Intent(getApplicationContext(), MainActivity.class);
home.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(home);
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
Intent intent = new Intent("Data");
intent.putExtra("messageFromCloud", remoteMessage.getNotification().getBody());
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
broadcaster.sendBroadcast(intent);
if (intent.getExtras() != null)
{
RemoteMessage.Builder builder = new RemoteMessage.Builder("MyFirebaseMessagingService");
for (String key : intent.getExtras().keySet())
{
builder.addData(key, intent.getExtras().get(key).toString());
}
onMessageReceived(builder.build());
}
}
}
}
If you want use MainActivity as Activity ofNotification click activity then you need to code like:
<activity
android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="gizmos"
android:scheme="example" />
</intent-filter>
</activity>
It will be automatically redirect to MainAcitivity on Notification click.
You need to mention The messaging file in manifest but this is not only the solution for open an activity through notification you need to write into intent in Messaging files.
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
For manifest write this code:
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="#color/colorAccent" />
<service
android:name=".MyFirebaseMessagingService"
android:permission=""
tools:ignore="ExportedService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
You need to declare it as service because notification is the part of service and run in application background when you application became closed.
I am trying to pass "badge" numbers via FCM.
I know that onMessageReceived() only get called when :
[payload contains only "data", no message]
I am using "com.google.firebase:firebase-messaging:11.0.2"
and pyfcm on server side to send my notification by this.
result = push_service.notify_multiple_devices(
registration_ids=ids, data_message={'badge': '5'}
)
>> all success.
My Service :
public class MyFCMService extends FirebaseMessagingService {
private static final String TAG = "MyFCMService";
public MyFCMService() {
}
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, remoteMessage.getData().toString());
super.onMessageReceived(remoteMessage);
}
}
My Service in manifest.xml:
<service android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service
android:name=".MyFCMService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
I have read this but I didn't close the app (just press HOME)
but still, logcat(verbose) shows nothing.
Is there any possible reasons?
I found the reason while copying my code in Manifest file...
it should be
<application>
...
<service
android:name=".libs.fcm.MyFCMService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
</application>
NOT
<service
android:name=".libs.fcm.MyFCMService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<application>
...
</application>
result:
D/MyFCMService: {badge=5}
and badge number can be update by ShortcutBadger through notification
with a datapayload contains badge.
pyfcm:
result = push_service.notify_multiple_devices(registration_ids=registration_ids, data_message=data_message, click_action=click_action, message_title=message_title,message_body=message_body, message_icon=message_icon)
Android Service
#Override
public void handleIntent(Intent intent) {
if(intent.hasExtra("badge"))
}
This simple example works strangely. If my test app is running (on foreground), all push are received fine. If I close my app, after some time, push is no longer received.
I send all push from Firebase console with a high priority. Tried it on Android 5 and 6.
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
sendNotification(remoteMessage.getData().get("message"));
}
}
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
#Override
public void onTokenRefresh() {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
sendRegistrationToServer(refreshedToken);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.firebase.quickstart.fcm">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name="com.google.firebase.quickstart.fcm.MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- [START firebase_service] -->
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<!-- [END firebase_service] -->
<!-- [START firebase_iid_service] -->
<service
android:name=".MyFirebaseInstanceIDService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<!-- [END firebase_iid_service] -->
</application>
I've successfully integrated Parse.com to android app & i can receive the push notification from Data browser and send the notification from android to ios ok.
When i tried to send the notification from ios to android, android cannot receive in my customize MyPushReceiver.
Installation on Parse is written with deviceToken.
I'm using Parse v1.10.2
Here is manifest:
<receiver
android:name=".push.MyPushReceiver"
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.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<receiver
android:name="com.parse.GcmBroadcastReceiver"
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.mypackage" />
</intent-filter>
</receiver>
Here is code in Application:
Parse.enableLocalDatastore(this);
Parse.initialize(this, PARSE_APPLICATION_ID, PARSE_CLIENT_ID);
ParseFacebookUtils.initialize(this);
ParsePush.subscribeInBackground("global", new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
}
}
});
ParseInstallation currentInstallation = ParseInstallation.getCurrentInstallation();
currentInstallation.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
}
}
});
Help me!