android firebase notification example

      No Comments on android firebase notification example

android firebase notification example

android firebase notification example

android firebase notification example is best options to keep your android app user active. Today it is observed that due to busy schedule people forget to use app which is already installed. Try to send at least one notification daily to keep your user active.

android firebase has changed the way of notification and now its very easy for all android developer to integrate notification system of firebase. I remembered that when I have tried with GCM first time it takes around 15 days, But firebase integration can be done in just one day, Let’s Start!

Step 1: Start Android Studio
Step 2: Go to Tools > Firebase (You will find Firebase Assistance in Right Panel)
Step 3: Click on Cloud Messaging and Set up Firebase Cloud Messaging
Step 4: Click on Connect app to firebase. (If you are connnecting first time then I suggest you to goto https://console.firebase.google.com and Login with your Google username and password, Accept Terms and Condition)
Step 5: Now you need to create project, Write down Project Name and Submit.
Step 6: Click Add FCM to your app.

(Here firebase assistance will automatically add related dependencies to your build.gradle module file)

(If your app stops and generate errors due to version mismatch then copy same version from play service to firebase)

(Current: Play Service: com.google.android.gms:play-services:11.8.0, Current Firebase : com.google.firebase:firebase-messaging:11.4.0, New Firebase: com.google.firebase:firebase-messaging:11.8.0)

(Still you face compilation or apk generation error, make sure to set minifyEnabled true and multiDexEnabled true)

Step 7: Now your android project is ready with dependencies, It’s time to start coding part.

Step 8: Add following class and it’s android manifest entry. (This will be used to generate token number save this token number into preference, Later on from other service or activity transfer this token to your server so you can send notification to individual using this token.)

public class AspTokenGenerator extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {

String refreshedToken = FirebaseInstanceId.getInstance().getToken();
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(“token”, refreshedToken).apply();
}
}

<service
android:name=”.AspTokenGenerator”>
<intent-filter>
<action android:name=”com.google.firebase.INSTANCE_ID_EVENT”/>
</intent-filter>
</service>

Step 9: Add following class and it’s android manifest entry. (This will be used to receive message from notification server and take actions like display message or do some background task)

public class AspMyFirebaseInstanceIDService extends FirebaseMessagingService {
public AspMyFirebaseInstanceIDService() {
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

if (remoteMessage.getData().size() > 0) {
String value1 = remoteMessage.getData().get(“key1”).toString();
// Process this value1 as per your requirements.

}
}
}
}

<service
android:name=”.AspMyFirebaseInstanceIDService”>
<intent-filter>
<action android:name=”com.google.firebase.MESSAGING_EVENT”/>
</intent-filter>
</service>

Step 10: Now only one step remaining, In step 8 we have stored token into preference, So we need to upload token to our server using our MainActivity on resume (Which activity you are sure will be opend by user)

(Add AsyncTask to fetch token from preference and upload to server.)

Step 11: It’s time to send message from android firebase console. Goto Notification >> New Message. Write down message, Select User Segment (Target: App and select your app package)

Step 12: It’s Time to Rock!, Send Now (This message will be received by All users, If you want to send to specific then you need to select single device and it’s token number.)

Want to Track User’s Move check android firebase analytics example. Share your experience regarding android firebase notification example with us and Stay Tuned !

PS:
If you like my article, Please share with your friends on Facebook, WhatsApp, Twitter, etc. Thanks in Advance.