Firebase onMessageReceived() not called via pyfcm

408 Views Asked by At

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?

1

There are 1 best solutions below

0
On

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