In this example, I will teach you, how to send notification
from android to android with the help of PHP.
First of all, create account in Google firebase link here
and follow instruction.
and get following file and past it in android app folder
google-services.json
Now, add following library in app.gradle file
dependencies {
compile 'com.google.firebase:firebase-messaging:9.6.1'
}
apply plugin: 'com.google.gms.google-services'
Like this
Class Name : MyFirebaseInstanceIDService.javaimport android.content.SharedPreferences;import com.esteth.app.util.App;import com.esteth.app.util.Prefs;import com.esteth.app.util.UserModel;import com.esteth.app.web.HttpCaller;import com.google.firebase.iid.FirebaseInstanceId;import com.google.firebase.iid.FirebaseInstanceIdService;import com.google.gson.Gson;import org.json.JSONException;import org.json.JSONObject;public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {private static final String TAG = "MyFirebaseIIDService";@Overridepublic void onTokenRefresh() {// Get updated InstanceID token.String refreshedToken = FirebaseInstanceId.getInstance().getToken();Log.d(TAG,"REFRESHED TOKEN: " + refreshedToken);// Sync this fcm token in you mysql database user table}private void sendRegistrationToServer(String token) {// TODO: Implement this method to send token to your app server.}}put this class in your codeClass Name : MyFirebaseMessagingService.javaimport android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.media.RingtoneManager;import android.net.Uri;import android.support.v4.app.NotificationCompat;import android.util.Log;importcom.google.firebase.messaging.FirebaseMessagingService;import com.google.firebase.messaging.RemoteMessage;import com.google.gson.JsonArray;public class MyFirebaseMessagingService extends FirebaseMessagingService {@Overridepublic void onMessageReceived(RemoteMessage remoteMessage) {Log.d(TAG,"From: " + remoteMessage.getFrom());// Check if message contains a data payload.if(remoteMessage.getData().size() > 0) {Log.d(TAG, "Message data payload: " + remoteMessage.getData());}Map<String,String> data =remoteMessage.getData();String type=data.get("type");showNotification(title);}private void showNotification(String title) {Intent intent = new Intent(this, YourActivity.class);intent.setAction(Intent.ACTION_MAIN);intent.addCategory(Intent.CATEGORY_LAUNCHER);PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);Uri defaultSoundUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_notification_icon).setContentTitle(title).setWhen(System.currentTimeMillis()).setContentIntent(pendingIntent).setAutoCancel(true).setSound(defaultSoundUri);NotificationManager notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);notificationManager.notify(1, notificationBuilder.build());}}Put following obove two class in your code and also declare in manifest file like followingWhen Notification come from other mobile, you will get notification value in MyFirebaseMessagingService. onMessageReceived()In Part 2, you can see how ho send and receive Notification through PHP.
No comments:
Post a Comment