如何从服务器推送通知到Android Mobile [英] How to do push notification from server to android mobile

查看:64
本文介绍了如何从服务器推送通知到Android Mobile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对推送通知一无所知.我在尝试学习.但我不明白.

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

我在服务器系统中有一个表MySQL数据库.在表格中进行任何更改后,我想在Android移动应用上显示通知.

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?

推荐答案

最近实际上实际上主要用于在您的项目中推送通知FCM....生成推送通知的最佳链接:链接

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

执行推送通知的步骤-适用于Android的Firebase云消息传递教程

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

  1. 转到firebase控制台并创建一个新项目.
  2. 现在输入您的应用名称并选择您的国家/地区.
  3. 现在单击将Firebase添加到您的Android应用".
  4. 现在您必须输入项目包名称,然后单击添加APP".
  5. 点击添加应用后,您将获得google-services.json文件.

在应用程序方面

  1. 现在回到您的android项目.转到应用程序文件夹并粘贴google-services.json文件
  2. 现在转到您的根目录build.gradle文件并添加以下代码.

  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.添加此行classpath'com.google.gms:google-services:3.0.0'

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

b.添加此行编译'com.google.firebase:firebase-messaging:9.0.0'

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

现在同步您的项目.

创建一个名为MyFirebaseInstanceIDService.java的类,并编写以下代码:

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 
    }
}

  • 现在创建MyFirebaseMessagingService.java并编写以下代码:

  • 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());
        }
    }
    

  • 现在,我们必须在AndroidManifest.xml文件中定义以上服务.因此,请按照以下说明进行清单和修改.

  • 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>
    

    最后

    转到firebase控制台,然后选择您创建的应用.从左侧菜单中选择通知.单击新消息.输入消息,选择单个设备并粘贴您复制的令牌,然后单击发送.与我在视频中所做的相同,然后检查您的设备

    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

    这篇关于如何从服务器推送通知到Android Mobile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆