How to do push notification from server to android mobile

78,521

Solution 1

actually now recently mostly use for push notification FCM inside that u project .... best link for build the push notication: link

steps for perform push notification - Firebase Cloud Messaging Tutorial for Android

  1. Go to firebase console and create a new project.
  2. Now put your app name and select your country.
  3. Now click on Add Firebase to Your Android App.
  4. Now you have to enter your projects package name and click on ADD APP.
  5. After clicking add app you will get google-services.json file.

On App side

  1. Now come back to your android project. Go to app folder and paste google-services.json file
  2. Now go to your root level build.gradle file and add the following code.

    a. Add this line classpath 'com.google.gms:google-services:3.0.0'

    b. Add this line compile 'com.google.firebase:firebase-messaging:9.0.0'

  3. Now sync your project.

  4. Create a class named MyFirebaseInstanceIDService.java and write the following code:

    public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
    
        private static final String TAG = "MyFirebaseIIDService";
    
        @Override
        public void onTokenRefresh() {
    
            //Getting registration token
            String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    
            //Displaying token on logcat 
            Log.d(TAG, "Refreshed token: " + refreshedToken);
    
        }
    
        private void sendRegistrationToServer(String token) {
            //You can implement this method to store the token on your server 
            //Not required for current project 
        }
    }
    
  5. Now create MyFirebaseMessagingService.java and write the following code:

    import 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;
    
    import com.google.firebase.messaging.FirebaseMessagingService;
    import com.google.firebase.messaging.RemoteMessage;
    
    /**
     * 
     */
    
    public class MyFirebaseMessagingService extends FirebaseMessagingService {
    
        private static final String TAG = "MyFirebaseMsgService";
    
        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            //Displaying data in log
            //It is optional 
            Log.d(TAG, "From: " + remoteMessage.getFrom());
            Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
    
            //Calling method to generate notification
            sendNotification(remoteMessage.getNotification().getBody());
        }
    
        //This method is only generating push notification
        //It is same as we did in earlier posts 
        private void sendNotification(String messageBody) {
            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);
    
            Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("Firebase Push Notification")
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);
    
            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
            notificationManager.notify(0, notificationBuilder.build());
        }
    }
    
  6. Now we have to define the above services in our AndroidManifest.xml file. So go to manifest and modify as follows.

    <!-- Adding Internet Permission -->
    
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    
        <!-- 
            Defining Services
        -->
        <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>
    </application>
    

finally

Go to firebase console and select the app you created. From the left menu select notification. Click on new message. Enter message, select single device and paste the token you copied and click on send. The same as I did on the video, and check your device

Solution 2

here is a good explanation about this:
http://quickblox.com/developers/SimpleSample-messages_users-android

The overall steps are:

  1. Create a google API project
  2. Enable push notifications for the project and get a API key
  3. Get a registration ID through android app (each device has a registration ID for a specific application)
  4. Create a server application to send your push messages as push notifications through google servers by GSM
  5. Create a notification when you get the push notification on the application side

It's not something i can write all here by details. Use Google for every step.

Solution 3

The first thing - Google Push Notifications are called GCM (Google Cloud Messaging). Wrong name usage might lead you to wrong information or tutorial. The other thing, you should rely on Developer. In this case start from Google Developers website, where you will find most of basic info and code examples to start with. https://developers.google.com/cloud-messaging/.

Update

GCM is deprecated, you should use Firebase Cloud Messaging (FCM)

Solution 4

You can check out Firebase... Check out this link

https://firebase.google.com/docs/cloud-messaging/

This link is sufficient to learn about Push Notifications

And as for sending notification when data in database changes, make your API send a request to FCM server so that it delivers necessary data to clients.

Share:
78,521

Related videos on Youtube

naveen
Author by

naveen

Updated on December 01, 2021

Comments

  • naveen
    naveen over 2 years

    I don't know anything about push notification. I am trying to learn. but I don't understand.

    I have one table MySQL database in server system. When any changes are made in the table I want display notification on an android mobile app.

    Can anyone provide any suggestions?

    • user370305
      user370305 about 12 years
    • naveen
      naveen about 12 years
      i logged in c2dm application i filled singup then i got message from google. after that what can i do. i don't know please tell me the solution. i facing the problem past 7 days.. please help me
    • user370305
      user370305 about 12 years
      Look at the link I given on above comment..
    • naveen
      naveen about 12 years
      i sea ur's above comment.but i cannot understand please tell me the procedures . i can follow ur's procedures.. please tell me
    • Zaz Gmy
      Zaz Gmy about 12 years
    • Raghav
      Raghav about 12 years
      Which server side language are you using???
    • Developine
      Developine almost 8 years
      codingaffairs.blogspot.com/2016/06/firebase-cloud-messaging-‌​push.html
  • Balu mallisetty
    Balu mallisetty over 3 years
    Thanks! I was searching for this from almost 2 days. Still no clue why google allowed fire base to handle notifications 🥴
  • Yohanim
    Yohanim over 3 years
    Warning, this answer already 5 years old. to get new token override onNewToken from FirebaseMEssagingService