how to send notification to android app from server

Hiii everyone in this article I am sharing how to send notification to the android app from the server. Now we’ll create a simple app that receives a firebase push notification from both the firebase console and from the PHP code. It’s very easy to used google FCM for push notification in android.

1. First thing you need to do is go to https://firebase.google.com/ and make an account to gain access to their console. After you gain access to the console you can start by creating your first project.

 

If you choose to create a new project, you need to set the project name. For example, 

 

 Select “Add Firebase to your Android app”

 

 

 

 

Add App package name and. I only set my package name and SHA-1 because I don’t use Firebase for my app’s authentication.

 

Click the ADD APP button here to download google-services.json. This is an important file and you will need to put it into your app.

2. Add google-services.json to your app folder

Replace the google-services.json in your app folder. The Google services plugin for Gradle will load the google-services.json file you just downloaded.

3. Configure gradle files

Open Android Studio and modify your build.gradle files to use the Google services plugin.

3.1  Add the Google services plugin in your Project-based build.gradle.

classpath 'com.google.gms:google-services:4.0.1'

3.2 Add the Google FCM plugin in your App Base build.gradle. 

compile 'com.google.firebase:firebase-messaging:11.0.4'
compile 'com.google.android.gms:play-services-base:11.0.4'
}
apply plugin: 'com.google.gms.google-services'

 4. Then add it into the AndroidManifest.xml file.

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

 

After you can create a java class for FirebaseMessagingService. and generate a token key for used backend Noficiation

5. Add a service that extends  MyFirebaseInstanceIDService  java class

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    private static final String TAG = "MyAndroidFCMIIDService";

    @Override
    public void onTokenRefresh() {
        //Get hold of the registration token
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        SessionManagement sessionManagement = new SessionManagement();
        sessionManagement.setDeviceID(refreshedToken,this);
        //Log the token
        Log.d(TAG, "Refreshed token: " + refreshedToken);
    }
    private void sendRegistrationToServer(String token) {
        //Implement this method if you want to store the token on your server
    }
}

6. Add a Notification Handel java class  MyFirebaseMessagingService  java class

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "MyAndroidFCMService";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
            createNotification(remoteMessage.getData().get("msg"));
        }

        //create notification

        //Calling method to generate notification
        if(remoteMessage.getNotification()!=null){
            //Log data to Log Cat
            Log.d(TAG, "From: " + remoteMessage.getFrom());
            Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
            createNotification(remoteMessage.getNotification().getBody());

        }
    }

    private void createNotification( String messageBody) {
        Intent intent = new Intent( this , HomeActvity. class );
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent resultIntent = PendingIntent.getActivity( this , 0, intent, PendingIntent.FLAG_ONE_SHOT);
        Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        long[] v = {200,500};
        NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder( this)
                .setSmallIcon(R.mipmap.ic_launcher2)
                .setColor(Color.BLUE)
                .setContentTitle("Codeplayon.com")
                .setContentText(messageBody)
                .setAutoCancel(false)
                .setSound(notificationSoundURI)
                .setVibrate(v)
                .setContentIntent(resultIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, mNotificationBuilder.build());
    }
}

complete integration Test and send your first push notification

you can go your firebase console and login open your project and inside the navigation bar select Cloud Messaging

   k

Happy Coding!!!!! All the Best

 

You may also like...