android - 如何从服务器推送通知到安卓手机

标签 android push-notification

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

我在服务器系统中有一个表 MySQL 数据库。当表格中发生任何更改时,我希望在 android 移动应用上显示通知。

谁能给点建议?

最佳答案

实际上现在最近主要用于该 u 项目中的推送通知 FCM .... 构建推送通知的最佳链接:link

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

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

在应用端

  1. 现在回到你的 android 项目。转到 app 文件夹并粘贴 google-services.json 文件
  2. 现在转到您的根级 build.gradle 文件并添加以下代码。

    一个。添加这一行 类路径 'com.google.gms:google-services:3.0.0'

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

  3. 现在同步您的项目。

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

    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. 现在创建 MyFirebaseMessagingService.java 并编写以下代码:

    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. 现在我们必须在我们的 AndroidManifest.xml 文件中定义上述服务。所以去manifest里面修改如下。

    <!-- 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 控制台并选择您创建的应用。 从左侧菜单中选择通知。 点击新消息。 输入消息,选择单个设备并粘贴您复制的 token ,然后单击发送。和我在视频上做的一样,检查你的设备

关于android - 如何从服务器推送通知到安卓手机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11151894/

相关文章:

ios - 在后台模式下,FCM 自定义声音在 iOS 上不起作用

ios - 在 TestFlight 中测试推送通知

Android+PhoneGap 致命异常

android - 如何解决 "This campaign targets 0 app user(s) (0% of potential users)."(Firebase) (Android)

java - 数据库值未更新 (Android)

iphone - 推送通知停止在我的实时应用程序上工作

android - Onesignar 从通知 android 获取标签?

android - 如何检测 View 的高度?

android - 问题 : specified child already has a parent. 您必须先对 child 的 parent 调用 removeView()

iOS 9 : Different push tokens returned for the same app (between installes)