-
In your app project, open the file
AndroidManifest.xml
. Add the following code after theapplication
opening tag:<service android:name=".ToDoMessagingService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter> </service> <service android:name=".ToDoInstanceIdService"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> </intent-filter> </service>
-
Open the file
ToDoActivity.java
, and make following changes:-
Add the import statement:
import com.google.firebase.iid.FirebaseInstanceId;
-
Change the definition of
MobileServiceClient
from private to private static, so it now looks like this:private static MobileServiceClient mClient;
-
Add
registerPush
method:public static void registerPush() { final String token = FirebaseInstanceId.getInstance().getToken(); if (token != null) { new AsyncTask<Void, Void, Void>() { protected Void doInBackground(Void... params) { mClient.getPush().register(token); return null; } }.execute(); } }
-
Update the onCreate method of the
ToDoActivity
class. Make sure to add this code after theMobileServiceClient
is instantiated.registerPush();
-
-
Add a new class to handle notifications. In Project Explorer, open the app > java > your-project-namespace nodes, and right-click the package name node. Click New, and then click Java Class. In Name, type
ToDoMessagingService
, and then click OK. Then, replace the class declaration with:import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.RemoteMessage; public class ToDoMessagingService extends FirebaseMessagingService { private static final int NOTIFICATION_ID = 1; @Override public void onMessageReceived(RemoteMessage remoteMessage) { String message = remoteMessage.getData().get("message"); if (message != null) { sendNotification("Notification Hub Demo", message); } } private void sendNotification(String title, String messageBody) { PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, ToDoActivity.class), 0); Notification.Builder notificationBuilder = new Notification.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle(title) .setContentText(messageBody) .setContentIntent(contentIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); if (notificationManager != null) { notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build()); } } }
-
Add another class to handle token updates. Create
ToDoInstanceIdService
java class and replace the class declaration with:import com.google.firebase.iid.FirebaseInstanceIdService; public class ToDoInstanceIdService extends FirebaseInstanceIdService { @Override public void onTokenRefresh() { ToDoActivity.registerPush(); } }
Your app is now updated to support push notifications.