Android Question Trying out Firebase Cloud Messaging

microbox

Active Member
Licensed User
Longtime User
Hi, I'm trying to learn how the Firebase cloud messaging works...but not really sure what to do next.
So far this is what done so far.

1. Open Android SDK Manager and install the latest versions of:

- Android Support Repository
- Google Repository

SS-2016-06-12_18.21.19.png


2. Register with Firebase and create a new project: https://firebase.google.com/docs/
Make sure that the package name matches your app's package name.

3. Download google-services.json and put it in the projects folder (folder of the b4a file).

4. Add the manifest snippets to the manifest editor based on the services that you need.

Below is what I have in my manifest file.
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="19"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.
'************ 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)************

I hope someone can tell What will be next step(s)?

Kind regards,
Joe
 

microbox

Active Member
Licensed User
Longtime User
You also need Google Play Services.
Hi Pendrush, Thanks..just updated my manifest and added the Google Play Services.
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="19"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.
'************ 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)************
Maybe my next question is how can I use it in my app?

Thanks again for the time,
Joe
 
Upvote 0

An Schi

Well-Known Member
Licensed User
Follow the tutorial. In your reciever app you want to add the firebase service and maybe a notification to show it.
Then you need a sender. You can send the push from the firebase console, but the recommended way is to use an API call which you can do with a b4j or b4a app.
 
Upvote 0

microbox

Active Member
Licensed User
Longtime User
Follow the tutorial. In your reciever app you want to add the firebase service and maybe a notification to show it.
Then you need a sender. You can send the push from the firebase console, but the recommended way is to use an API call which you can do with a b4j or b4a app.
Hi An Schi... I'm really new to this :confused: is the code below what I need for testing?
B4X:
Sub Process_Globals
   Private fm As FirebaseMessaging
End Sub

Sub Service_Create
   fm.Initialize("fm")
End Sub

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

Sub Service_Start (StartingIntent As Intent)
   If fm.HandleIntent(StartingIntent) Then Return
End Sub

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

Sub Service_Destroy

End Sub

Thanks,
Joe
 
Upvote 0

An Schi

Well-Known Member
Licensed User
Yes. That's the firebase service you need in your reciever app. Add a new service module to your project and copy & paste this into it.
For the first tests you can use the firebase console to push.
 
Upvote 0

microbox

Active Member
Licensed User
Longtime User
Yes. That's the firebase service you need in your reciever app. Add a new service module to your project and copy & paste this into it.
For the first tests you can use the firebase console to push.

I think I'm still lacking something. After compiling and sending test message from firebase console, I did not receive any yet.

PushNot.png
 
Upvote 0

microbox

Active Member
Licensed User
Longtime User
Follow the tutorial!
It says:
You didn't name your service like this.

Hey An Schi...thanks :) I think I'm receiving the notification. But I have another question if you don't mind. :)
How can I see the content message?
 
Upvote 0

microbox

Active Member
Licensed User
Longtime User
I finally got it...at first it was showing null...but after several attempt messages arrived. Thanks Pendrush and An Schi
 
Upvote 0

microbox

Active Member
Licensed User
Longtime User
Thanks DonManfred I updated Google Repository.

I tried my demo application today but no messages arrived :confused: I use firebase console. Any idea what I'm missing?
 
Upvote 0

microbox

Active Member
Licensed User
Longtime User
Hi Erel, I placed my API Key but I'm not receiving any message. Probably I'm still missing something in the B4J code.
Debug output:
B4X:
Waiting for debugger to connect...
Program started.
[jobname=fcm, success=true, username=
, password=, errormessage=, target=class b4j.example.main
, taskid=1, req=anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpRequest@70f11, tag=java.lang.Object@16e523d
, httputils2service=null]
{"message_id":7907454354097248626}
Program terminated (StartMessageLoop was not called).
 
Upvote 0
Top