49

プッシュ通知についてはわかりません。私は学ぶことをしようとしています。しかし、私は理解していません。

サーバーシステムに 1 つのテーブル MySQL データベースがあります。テーブルに変更が加えられたら、Android モバイル アプリに通知を表示します。

誰でも提案を提供できますか?

4

4 に答える 4

9

実際には最近、主にそのuプロジェクト内でFCMをプッシュ通知に使用しています....プッシュ通知を構築するための最良のリンク:リンク

プッシュ通知を実行する手順 - Firebase Cloud Messaging Tutorial for Android

  1. firebase コンソールに移動し、新しいプロジェクトを作成します。
  2. アプリ名を入力し、国を選択します。
  3. [Firebase を Android アプリに追加] をクリックします。
  4. ここで、プロジェクトのパッケージ名を入力して、[アプリの追加] をクリックする必要があります。
  5. [アプリの追加] をクリックすると、google-services.json ファイルが取得されます。

アプリ側

  1. Android プロジェクトに戻ります。app フォルダーに移動し、google-services.json ファイルを貼り付けます
  2. ルート レベルの build.gradle ファイルに移動し、次のコードを追加します。

    a. この行クラスパス「com.google.gms:google-services:3.0.0」を追加します

    b. この行を追加 compile '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 ファイルで上記のサービスを定義する必要があります。したがって、マニフェストに移動して、次のように変更します。

    <!-- 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 コンソールに移動し、作成したアプリを選択します。左側のメニューから [通知] を選択します。新しいメッセージをクリックします。メッセージを入力し、単一のデバイスを選択して、コピーしたトークンを貼り付け、[送信] をクリックします。ビデオで行ったのと同じように、デバイスを確認します

于 2016-11-21T09:45:45.897 に答える
8

これについての良い説明があります:
http://quickblox.com/developers/SimpleSample-messages_users-android

全体的な手順は次のとおりです。

  1. Google API プロジェクトを作成する
  2. プロジェクトのプッシュ通知を有効にして API キーを取得する
  3. Android アプリから登録 ID を取得します (各デバイスには特定のアプリケーションの登録 ID があります)。
  4. GSM によって Google サーバーを介してプッシュ通知としてプッシュ メッセージを送信するサーバー アプリケーションを作成します。
  5. アプリ側でプッシュ通知を受け取ったら通知を作成する

ここにすべてを詳細に書くことはできません。すべてのステップで Google を使用します。

于 2016-08-30T04:50:19.637 に答える
8

まず、Google プッシュ通知は GCM (Google Cloud Messaging) と呼ばれます。間違った名前を使用すると、間違った情報やチュートリアルにつながる可能性があります。もう 1 つは、Developer に頼る必要があります。この場合は、Google Developers の Web サイトから始めてください。そこには、基本的な情報とコード例のほとんどが掲載されています。https://developers.google.com/cloud-messaging/ .

アップデート

GCMは非推奨です。Firebase Cloud Messaging (FCM)を使用する必要があります

于 2016-10-30T10:29:39.173 に答える
4

Firebaseをチェックアウトできます...このリンクをチェックしてください

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

このリンクは、プッシュ通知について学ぶのに十分です

また、データベースのデータが変更されたときに通知を送信する場合は、API から FCM サーバーにリクエストを送信して、必要なデータをクライアントに配信します。

于 2016-10-30T10:29:32.853 に答える