Configure Firebase push messages with Maven Androi

2019-06-13 20:38发布

问题:

Is there a way to properly configure Firebase messaging with Maven without using Gradle?

I've added this dependency to pom.xml

<dependency>
    <groupId>com.google.firebase</groupId>
    <artifactId>firebase-messaging</artifactId>
    <version>9.0.2</version>
    <type>aar</type>
</dependency>

And modified my AndroidManifest.xml by copying configs from AndroidManifest.xml file located in firebase AAR. I didn't find the way to filter them during maven buld using filtering resources or replacer plugin like gradle plugin do.

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="<MY_APP_PACKAGE>.permission.C2D_MESSAGE"  android:protectionLevel="signature" />
<uses-permission android:name="<MY_APP_PACKAGE>.permission.C2D_MESSAGE" />

<!-- copied from firebase common module to be able to set <MY_APP_PACKAGE>
     (that is set during compilation with gradle plugin)  --> 
<provider
        android:authorities="<MY_APP_PACKAGE>.firebaseinitprovider"
        android:name="com.google.firebase.provider.FirebaseInitProvider"
        android:exported="false"
        android:initOrder="99" />
<!-- copied from firebase iid module to be able to set <MY_APP_PACKAGE>
     (that is set during compilation with gradle plugin)  --> 
<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="<MY_APP_PACKAGE>" />
    </intent-filter>
</receiver>
<service android:name=".service.PushMessageService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".service.GoogleIDService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
    </intent-filter>
</service>

Also, added string resources (that auto set by gradle plugin from config json file)

<string name="google_app_id" translatable="false">...</string>
<string name="google_api_key" translatable="false">...</string>
<string name="firebase_database_url" translatable="false">...</string>
<string name="ga_trackingId" translatable="false">...</string>
<string name="gcm_defaultSenderId" translatable="false">...</string>
<string name="google_storage_bucket" translatable="false">...</string>

After running the app I had these logs

D/FirebaseApp﹕ com.google.firebase.auth.FirebaseAuth is not linked. Skipping initialization.
D/FirebaseApp﹕ Initialized class com.google.firebase.iid.FirebaseInstanceId.
D/FirebaseApp﹕ com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
D/FirebaseApp﹕ com.google.android.gms.measurement.AppMeasurement is not linked. Skipping initialization.
I/FirebaseInitProvider﹕ FirebaseApp initialization successful

The docs says Firebase will request new token automatically but I never receive onTokenRefresh callback Is there anything I missed?

P.S: Please don't suggest using Gradle....

Any help is appreciated... Thanks

回答1:

Your custom implementation is almost correct. You are missing this:

<receiver       
android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver"
android:exported="false" />

BUT.. I have to warn you that the approach of manually modifying the AndroidManifest is highly discouraged. Since the manifest is automatically configured by Gradle we consider it as an internal implementation details and not officially supported API.

Long story short: the manifest configuration could change at any release, and your app would break if you don't update your manifest accordingly.



回答2:

The issue resolved, thanks Diego

I just wanted to add some additional configs to AndroidManifest.xml for someone who will perform same issue

(all copied from firebase AndroidManifest.xml

<!-- Internal (not exported) receiver used by the app to start its own exported services
     without risk of being spoofed. -->
<receiver
        android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver"
        android:exported="false" />

<!-- FirebaseInstanceIdService performs security checks at runtime,
     no need for explicit permissions despite exported="true" -->
<service
        android:name="com.google.firebase.iid.FirebaseInstanceIdService"
        android:exported="true">
    <intent-filter android:priority="-500">
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
    </intent-filter>
</service>

<!-- FirebaseMessagingService performs security checks at runtime,
     no need for explicit permissions despite exported="true" -->
<service
        android:name="com.google.firebase.messaging.FirebaseMessagingService"
        android:exported="true">
    <intent-filter android:priority="-500">
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>