Firebase onMessageReceived() not called via pyfcm - android

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"))
}

Related

onMessageReceived not triggered when app is in foreground

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.

firebase messaging onCreate not called

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..

FCM not receiving notification messages

I have my manifest in check, my services in check, my dependencies in check, class variables in check and still no notifications on my device. I'm using my computer to send firebase cloud messages and I should be receiving them on my android phone. However,my android phone is not picking up the notifications. And yes, my android device is in the background as I send the message. Anyone know what my problem is?
Here's the source code:
My Service
public class FirebaseIDMessage extends FirebaseMessagingService {
private static final String TAG = "FirebaseIDMessage";
#Override
public void onNewToken(String s) {
super.onNewToken(s);
String token = s;
Log.d(TAG, "Registered token: = " + token);
sendRegistrationToServer(token);
}
private void sendRegistrationToServer(String token){
}
}
My Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.messaging">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<!-- Services -->
<service android:name=".FirebaseIDMessage"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<activity android:name=".MessagingActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
My Activity
public class MessagingActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_messaging);
}
}
In your manifest file, you are missing INSTANCE_ID_EVENT
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
This is the intent-filter for the Class extended with FirebaseInstanceIdService
public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService
{
public static final String REGISTRATION_TOKEN = "REG_TOKEN";
#Override
public void onTokenRefresh()
{
String token = FirebaseInstanceId.getInstance().getToken();
Log.e(REGISTRATION_TOKEN,token);
}
}
So, finally the manifest service will be like
<service android:name=".Utils.PushNotification.MyFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
On the log you would have got registered Token. Your all set only if you receive that token.
Goto Firebase console : https://console.firebase.google.com
Choose your project -> From the left side menu -> Choose Cloud Messaging -> New Message
Type your message and then in the TARGET choose Single Device and then copy paste your received token and check on your device for this notification by this way we can confirm you have set all notification related code correctly. If you still face any problem please let know so that I can share some samples.

Android BroadcastReceiver to get all SDCARD Status

I'm trying to get all SD card statuses using the BroadcastReceiver, but the code isn't working correctly. The BroadcastReceiver does not give a comprehensive log after a change in SD card status, such as Turn on(off) USB Storage. Any ideas?
Manifest receiver actions:
<receiver android:name=".Broadcasts.BroadcastChangeSDCardStatus">
<intent-filter>
<action android:name="android.intent.action.MEDIA_REMOVED"/>
<action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
<action android:name="android.intent.action.MEDIA_EJECT"/>
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
</intent-filter>
</receiver>
OR this receiver defined:
<receiver android:name=".Broadcasts.BroadcastChangeSDCardStatus">
<intent-filter>
<action android:name="android.intent.action.ACTION_MEDIA_BAD_REMOVAL"/>
<action android:name="android.intent.action.ACTION_MEDIA_EJECT"/>
<action android:name="android.intent.action.ACTION_MEDIA_REMOVED"/>
<action android:name="android.intent.action.ACTION_MEDIA_UNMOUNTED"/>
</intent-filter>
</receiver>
BroadcastReceiver class to detect
public class BroadcastChangeSDCardStatus extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
boolean status = getStorageStatus();
Log.e("SDCARD Status: ", status + "");
}
public static boolean getStorageStatus() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return false;
} else {
return false;
}
}
}
<receiver android:name=".Broadcasts.BroadcastChangeSDCardStatus" >
<intent-filter>
<action android:name="android.intent.action.MEDIA_REMOVED" />
<action android:name="android.intent.action.MEDIA_MOUNTED" />
</intent-filter>
Above code should work but as you said you already tried this so it looks like something is wrong with your receiver name .Your broadcast receivers is not registered.Give receiver name complete package name with class name correctly
You also need to set the android:scheme to "file" :
Do this by adding <data android:scheme="file" /> to your receiver like:
<receiver android:name=".Broadcasts.BroadcastChangeSDCardStatus">
<intent-filter>
<action android:name="android.intent.action.MEDIA_REMOVED"/>
<action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
<action android:name="android.intent.action.MEDIA_EJECT"/>
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<data android:scheme="file" />
</intent-filter>
</receiver>
Hope this helps you out
i'm after adding this permission resolve my problem now:
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

prevent Android BootReceiver from starting main activity

Here´s my problem:
my Android app registers a boot receiver, which initializes a PushNotification-Manager (PushWoosh).
This is because even after a reboot of the device, the user should be able to receive push notifications without having to start the app manually.
This works, but when the device is rebooted, the apps main activity (MainMenuActivity) is launched and brought to the foreground, which should not happen.
Here´s the involved code:
AndroidManifest.xml:
<!-- Re-register PushManagers after reboot -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar">
<!-- PushWoosh -->
<activity android:name="com.arellomobile.android.push.PushWebview"/>
<activity android:name="com.arellomobile.android.push.MessageActivity"/>
<activity android:name="com.arellomobile.android.push.PushHandlerActivity"/>
<!-- PushWoosh -->
<receiver
android:name="com.google.android.gcm.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="de.myapp.android"/>
</intent-filter>
</receiver>
<!-- PushWoosh -->
<service android:name="com.arellomobile.android.push.PushGCMIntentService"/>
<!-- Boot-Receiever -->
<receiver android:name="de.myapp.android.startup.BootCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<activity
android:name="de.myapp.android.activity.MainMenuActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<!-- Starten bei Klick auf Launcher Icon -->
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<!-- Starten bei Erhalt einer Push Notification -->
<action android:name="de.myapp.android.MESSAGE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
BootCompleteReceiver.java:
public class BootCompleteReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent i) {
PushWooshHelper.setupPushNotifications(context.getApplicationContext());
}
}
PushWooshHelper.java:
public class PushWooshHelper {
public static void setupPushNotifications(Context context) {
PushManager pushManager = new PushManager(context, AppIDs.PUSHWOOSH_APP_ID, AppIDs.GCM_PROJECT_ID);
pushManager.onStartup(context);
pushManager.startTrackingGeoPushes();
}
}
MainMenuActivity.java:
public class MainMenuActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
...
}
private void checkMessage(Intent intent) {
if (intent != null) {
String log = "PUSH NOTIFICATION RECEIVED.";
if (intent.hasExtra(PushManager.PUSH_RECEIVE_EVENT)) {
log += "message: " + intent.getExtras().getString(PushManager.PUSH_RECEIVE_EVENT);
}
else if (intent.hasExtra(PushManager.REGISTER_EVENT)) {
log += "<register>";
}
else if (intent.hasExtra(PushManager.UNREGISTER_EVENT)) {
log += "<unregister>";
}
else if (intent.hasExtra(PushManager.REGISTER_ERROR_EVENT)) {
log += "<register error>";
}
else if (intent.hasExtra(PushManager.UNREGISTER_ERROR_EVENT)) {
log += "<unregister error>";
}
}
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
checkMessage(intent);
setIntent(new Intent());
}
}
Please note: I do not have access to PushManager.onStartup(), since it is provided by PushWoosh.
Any help is greatly appreciated!
That's strange. Pushwoosh uses GCM which works after restart of the device out of the box. You just have to register for the push notifications once and then after restart GCM takes care about that itself.

Categories

Resources