Hi Everyone,
I wil tell you about sending notification to your android application from the firebase notification cloud messaging service. It will be step by step and short explainings.
In Android Studio, add the FCM dependency to your app-level build.gradle file:
1 2 3 |
dependencies { compile 'com.google.firebase:firebase-messaging:11.2.0' } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<service android:name=".MyFirebaseMessagingService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter> </service> <service android:name=".MyFirebaseInstanceIDService"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> </intent-filter> </service> |
Create the following classes under your package folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { private static final String TAG = "MyFirebaseIDService"; @Override public void onTokenRefresh() { String token = FirebaseInstanceId.getInstance().getToken(); Log.d(TAG,"Token: "+token); sendRegistrationToServer(token); } private void sendRegistrationToServer(String token) { } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
public class MyFirebaseMessagingService extends FirebaseMessagingService { private static final String TAG = "MyFirebaseMsgService"; @Override public void onMessageReceived(RemoteMessage remoteMessage) { if (remoteMessage.getData().size() > 0) { Log.d(TAG, "Content of Meessage: " + remoteMessage.getData()); sendNotification("Farmasilove.com", "" + remoteMessage.getData()); } if (remoteMessage.getNotification() != null) { Log.d(TAG, "Head of Meessage: " + remoteMessage.getNotification().getTitle() + " " + "Content of Meessage: " + remoteMessage.getNotification().getBody()); sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody()); } } private void sendNotification(String title, String body) { Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); long[] pattern = {500, 500, 500, 500}; NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon) .setContentTitle(title) .setContentText(body) .setAutoCancel(true) .setVibrate(pattern) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); try { Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification); r.play(); } catch (Exception e) { e.printStackTrace(); } notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); } } |
Now click here to go to firebase console than add your project on this page with package name of your project. After adding the project clickour project and click the Notification section on screen.
Now click the “Send your firs message” button on the screen and start the send messages. That’s all.
Good days.