Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Integration using WakefulBroadcastReceiver

I integrated the push notification system with Firebase in my project and it's working well. Few clarficiation

Steps to follow the integration of Firebase

  1. Created the json file from firebase console and added in my project ( google-services.json )
  2. Dependency compile lib added in my build.gradel ( root as well as inside of app)

  3. AndroidManifest.xml added the following server.

    <service
    android:name="com.myfirebase.myfirebasemsgservice"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
     <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
    </service>
    
  4. Inside of the "myfirebasemsgservice" added the following line and proceeding and working well.

    public void onMessageReceived(RemoteMessage fcmMessage)
    {
    Log.d(TAG, "From: " + fcmMessage.getFrom());
    
     if (fcmMessage.getData().size() > 0) {
      Log.d(TAG, "Message data payload: " + fcmMessage.getData());
        Log.d(TAG, "Message data payload: " + fcmMessage.getData().toString());
    
  5. I checked and got an notification if sending from firebase console as well as ARC (Advanced REST CLIENT). I tried Notification Payload and Data Payload both are working well.

My clarification and how we need to integrate in firebase same like below code. How we did previously means?. Any idea and how we need to integrate like this?.

  1. AndroidManifest.xml - added previously.

                      <receiver
               android:name="com.mygcm.gcmbroadcastReceiver"
             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>
    
                <service android:name="com.mygcm.gcmIntentService" />
    
  2. gcmbroadcastReceiver added the below code.

          public class GcmBroadcastReceiver extends WakefulBroadcastReceiver 
       {
       @Override
          public void onReceive(Context context, Intent intent) {
        ComponentName comp = new ComponentName(context.getPackageName(),
        GCMIntentService.class.getName());
         startWakefulService(context, (intent.setComponent(comp)));
         }
         }
    
  3. gcmIntentService added the below code.

               public class gcmIntentService extends IntentService {
    
                @Override
                    protected void onHandleIntent(Intent intent) 
                  {
                 Bundle extras = intent.getExtras();
                  GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
                 String messageType = gcm.getMessageType(intent);
    
    try 
    {
        if (!extras.isEmpty()) 
        {
            if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) 
            {
                Log.d("LogData",intent.getStringExtra(Constants.PAYLOAD));
            } 
            else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) 
            {
                Log.d("LogData",intent.getStringExtra(Constants.PAYLOAD));
            } 
            else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) 
            {   
                Log.d("LogData",intent.getStringExtra(Constants.PAYLOAD));
            }
        }
    } 
    catch(Exception ignored)
    {
        ExceptionTrack exe_track=ExceptionTrack.getInstance();
    }
    finally {
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }
    

    }

like image 795
Saikumar Avatar asked Apr 09 '26 22:04

Saikumar


1 Answers

 <service
        android:name=".fcm.MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>
    <service
        android:name=".fcm.FireBaseInstanceID_Service">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>

App Gradle

compile 'com.google.firebase:firebase-messaging:9.4.0' apply plugin: 'com.google.gms.google-services'

Project Gradle classpath 'com.google.gms:google-services:3.0.0'

Log.e("token_id", FirebaseInstanceId.getInstance().getToken());

like image 199
PRATEEK BHARDWAJ Avatar answered Apr 12 '26 11:04

PRATEEK BHARDWAJ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!