Xamarin.Forms FCM OnRefereshToken Method not calle

2019-08-11 04:05发布

I have Implemented a Xamarin Pushnotification using FCM and my code looks like

 // This service handles the device's registration with FCM.
[Service]
[IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
public class ZCFirebaseIIDService : FirebaseInstanceIdService
{

    #region override methods
    public override void OnTokenRefresh()
    {
        var refreshedToken = FirebaseInstanceId.Instance.Token;
        App.DeviceToken = FirebaseInstanceId.Instance.Token;
    }
    #endregion
}

Also I have added all required permission in AndroidManifest and its like

<uses-sdk android:minSdkVersion="14" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:label="App name" android:icon="@drawable/icon">
    <provider android:name="android.support.v4.content.FileProvider" android:authorities="com.appname.fileprovider" android:exported="false" android:grantUriPermissions="true">
        <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths">
        </meta-data>
    </provider>
    <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" />
    <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true" 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="${applicationId}" />
        </intent-filter>
    </receiver>
</application>

it's perfectly fine in Debug mode but when I run the app in release mode OnTokenRefresh method not even called .

can anyone please suggest me how can I resolve this issue.

1条回答
Rolldiameter
2楼-- · 2019-08-11 04:11

There is a known bug in FCM that even I faced when I was implementing the same in my android application

Java.Lang.IllegalStateException: Default FirebaseApp is not initialized in this process Make sure to call FirebaseApp.initializeApp(Context) first.

Which is a bug that you will also find in the Microsoft xamarin website where it shows the implementation of FCM which you can find here

Also, there is a Bugzilla report on the same here.

Steps to solve the issue:

Xamarin only gives you this:

This is a known problem that you can work around by cleaning the solution and rebuilding the project (Build > Clean Solution, Build > Rebuild Solution). For more information, see this forum discussion.

But I recommend you do the following to be sure.

  • Check if you are receiving notifications when in debug mode.

  • If not remove bin and obj folders from your Android project.

  • Clean build your solution and try again

  • If it works archive the application and in release mode, test if you are receiving notifications.(If it works on one device it will work in all).

  • Steps for archive can be found here

查看更多
登录 后发表回答