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>
Related
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.
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"))
}
I have just implemented FCM push notification to my webview app, when I run my app on the emulator and send a notification from FCM console, my app will receive the notification when my app running in background. But when I build and apk file and install it on my gadget, the same notification will never receive on my gadget. How to fix this problem?
package com.example.intawebapp.activity;
import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
public class FirebaseIDService extends FirebaseInstanceIdService {
private static final String TAG = "FirebaseIDService";
#Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
// TODO: Implement this method to send any registration to your app's servers.
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
// Add custom implementation, as needed.
}
}
and
package com.example.intawebapp.activity;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
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());
}
}
and the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intawebapp">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".activity.MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
</activity>
<activity
android:name=".activity.SplashActivity"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".activity.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service android:name=".activity.FirebaseIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
</application>
</manifest>
My gadget can receive notification now after I put SHA 1 in FCM console
I am getting following error when trying to configure Push Notification:
Failed to resolve target intent service, skipping classname enforcement
Error while delivering the message: ServiceIntent not found.
Manifest:
<manifest package="com.packageName"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> <!-- support previous 4.4 KitKat devices-->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.packageName.permission.C2D_MESSAGE" />
<application
android:name="AppName"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#android:style/Theme.Black.NoTitleBar">
<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="com.packageName" />
</intent-filter>
<intent-filter> <!-- support previous 4.4 KitKat devices-->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.packageName" />
</intent-filter>
</receiver>
<service
android:name="com.packageName.activities.RegistrationIntentService"
android:exported="false" >
</service>
<service
android:name="com.packageName.activities.GcmIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<service
android:name="com.packageName.activities.MyGcmListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
</application>
MyGcmListenerService:
public class MyGcmListenerService extends GcmListenerService {
#Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
Log.d("From", "From: " + from);
Log.d("Msg", "Message: " + message);
}
}
RegistrationIntentService
public class RegistrationIntentService extends IntentService {
private static String SENDER_ID = "MySenderID";
public RegistrationIntentService(){
super(SENDER_ID);
}
#Override
public void onHandleIntent(Intent intent){
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
try {
InstanceID instanceID = InstanceID.getInstance(this);
String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
Log.i("TOKEN", "GCM Registration Token: " + token);
} catch (Exception e) {
Log.d("Fail token", "Failed to complete token refresh", e);
}
}
}
GcmIDListenerService
public class GcmIDListenerService extends InstanceIDListenerService {
#Override
public void onTokenRefresh() {
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}
}
I have placed google-services.json in projectName/app
I send push notification from web http://techzog.com/development/gcm-notification-test-tool-android/ and the response is success:1 but it not received in Android device.