Android Tutorial Setting up Firebase messaging in 5 minutes

Important for Huawei users (or others):

Make sure your energy settings don't pause your app (even the firebase service). Huawei calls it "protected apps". If your app isn't protected, the app will be completely paused when the screen is turned off! Yes, even the service! On my P8 I wondered why my Google Calendar widget did not update. That's the reason why.

Huwei OS has a list with "known apps" like fb and WhatsApp. Here it is set to protected automatically.

whatsapp-notifications-not-working-or-getting-delayed.jpg






This is a quick guide how to

- set up a fresh app
- creating a new firebase project in the Firebase console

to receive Firebase messages.

First follow Erel's tutorial: https://www.b4x.com/android/forum/t...-messages-firebase-cloud-messaging-fcm.67716/

Important here:

- add "Android Support Repository" and "Google Repository" via SDK-manager
- copy the firebase lib (jar & xml) to the additional lib's folder
- create a new app with Erel's code :)
- add FirebaseNotifications from the libs tab
- edit the manifest and add these lines

B4X:
'************ Google Play Services Base ************
AddApplicationText(
   <activity android:name="com.google.android.gms.common.api.GoogleApiActivity"
  android:theme="@android:style/Theme.Translucent.NoTitleBar"
  android:exported="false"/>
    <meta-data
  android:name="com.google.android.gms.version"
  android:value="@integer/google_play_services_version" />
)
'************ Google Play Services Base (end) ************
'************ Firebase Base ************
CreateResourceFromFile("google-services", "google-services.json")
AddPermission(android.permission.ACCESS_NETWORK_STATE)
AddPermission(android.permission.INTERNET)
AddPermission(android.permission.WAKE_LOCK)
AddPermission(com.google.android.c2dm.permission.RECEIVE)
AddPermission(${applicationId}.permission.C2D_MESSAGE)
AddManifestText( <permission android:name="${applicationId}.permission.C2D_MESSAGE"
  android:protectionLevel="signature" />)
AddApplicationText(
<receiver
  android:name="com.google.android.gms.measurement.AppMeasurementReceiver"
  android:enabled="true">
  <intent-filter>
  <action android:name="com.google.android.gms.measurement.UPLOAD"/>
  </intent-filter>
  </receiver>

  <service
  android:name="com.google.android.gms.measurement.AppMeasurementService"
  android:enabled="true"
  android:exported="false"/>
   <provider
  android:authorities="${applicationId}.firebaseinitprovider"
  android:name="com.google.firebase.provider.FirebaseInitProvider"
  android:exported="false"
  android:initOrder="100" />
    <receiver
  android:name="com.google.android.gms.measurement.AppMeasurementReceiver"
  android:enabled="true">
  <intent-filter>
  <action android:name="com.google.android.gms.measurement.UPLOAD"/>
  </intent-filter>
  </receiver>

  <service
  android:name="com.google.android.gms.measurement.AppMeasurementService"
  android:enabled="true"
  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>
    <receiver
  android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver"
  android:exported="false" />


  <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>
)
'************ Firebase Base (end) ************
'************ Firebase Notifications ************
AddApplicationText(
    <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>
   <service android:name="anywheresoftware.b4a.objects.FirebaseNotificationsService">
     <intent-filter>
  <action android:name="com.google.firebase.MESSAGING_EVENT"/>
     </intent-filter>
   </service>
)
'************ Firebase Notifications (end)************

Open your browser and go to https://console.firebase.google.com/

1.JPG


Click on "Create new project" and give it a name


Select "Add Firebase to your Android app"

2.JPG


Set your app's package name (B4A -> Build configurations)

3.JPG


In the console use THE SAME name:

4.JPG


No need to enter more at this moment! Just click on "Add app"

Save the google-services.json to the main folder of your app


5.JPG


Under the tab "Cloud Messaging" you see the Firebase Messaging token and the Server key. Both are good for the API-Key in the B4J-App in Erel's example:

B4X:
Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY) ' this goes here...

It is recommended to use the token (the "server key" is deprecated). Right now both work.

6.JPG


Compile your app (debug) and set a brakepoint here

B4X:
Public Sub SubscribeToTopics
   fm.SubscribeToTopic("general") 'you can subscribe to more topics
   log(fm.token)
End Sub

Copy the token (this is the device's token) to the clipboard.

For a quick test: In the Firebase console select your project and click on "notifications":

8.JPG


Paste the token and enter a message text

Run your app and press "Send message".

Your app will receive the message in a second (don't forget a breakpoint here):

B4X:
Sub fm_MessageArrived (Message As RemoteMessage)
   Log("Message arrived")
   Log($"Message data: ${Message.GetData}"$)
   Dim n As Notification
   n.Initialize
   n.Icon = "icon"
   n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
   n.Notify(1)
End Sub
 

Attachments

  • 7.JPG
    7.JPG
    15.6 KB · Views: 692
Last edited:

Dadaista

Active Member
Licensed User
Longtime User
Hi DonManfred thx for the reply

I've followed but I can not add the libraries to the libs tab!!!

In my libs tab only FirebaseAdmob is presents. (in the additional libs Dir)

no FirebaseAuth.jar or .xml, no FirebaseAnalytics.jar or .xml, no FirebaseNotificatios.jar or .xml in my computer!!

What can I do??:confused::confused:
 
Last edited:

Dadaista

Active Member
Licensed User
Longtime User
Oh!... lol!!!... my nerves make me blind !!!:oops::oops:

I just found the other libs!!!

thx again DonManfred!!! :):)
 
Top