2017年5月23日 星期二

Firebase Cloud Messaging 接收訊息 PHP Service

FCM兩種推播通知方式 - Notification與Data

----------------------------------FCM前置步驟可觀看----------------------------------
Step1:
https://litotom.com/2016/06/24/android-firebase-cloud-messaging-1/
注意:
apply plugin: 'com.google.gms.google-services'
要放在build.gradle(Module: app)這個檔案裡面的最下面

Step2:
https://litotom.com/2016/06/26/firebase-android-send-2/

-----------------------------------------------------------------------------------------------

1.在MyFirebaseMessagingService.java加入推播通知訊息
package com.terryyamg.fcmtest;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
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 = "MyFirebaseMessaging";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.i(TAG, "onMessageReceived:" + remoteMessage.getFrom());
        Intent intent = new Intent();   //點擊啟動到MainActivity頁面
        intent.setClass(this, MainActivity.class);
        showNotification(this, remoteMessage, intent);
    }

    // 顯示通知
    // remoteMessage.getData()         -  無論app在什麼狀態下皆會執行 MyFirebaseMessagingService(需對應Service設定的字串文字)
    // remoteMessage.getNotification() -  只會在app顯示時,執行app的接收訊息 MyFirebaseMessagingService
    private void showNotification(Context context, RemoteMessage remoteMessage, Intent intent) {
        NotificationManagerCompat manager = NotificationManagerCompat.from(this);
        PendingIntent iPending = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle(remoteMessage.getData().get("title")) //需對應Service設定的字串文字
                        .setWhen(System.currentTimeMillis())
                        .setDefaults(Notification.DEFAULT_SOUND)
                        .setContentText(remoteMessage.getData().get("body")) //需對應Service設定的字串文字
                        .setContentIntent(iPending)
                        .setAutoCancel(true);

        Notification notification = mBuilder.build();
        manager.notify(1, notification);

    }
}

2.Service推播 fcm_service.php
    #API access key from Google API's Console
    define( 'API_ACCESS_KEY', 'AAAA6iG_xzk:APA91bHxlAkx5byr66cIFk-CXAr2k3hvs4tGQV9nAzj2nw75kPC8_9eIyyZVaeUPYFYzAk10qV-D0Pwl5BsIGslvv32PI2ECap1KcmW7Iu6lyzh1A3eEE2TJ5jVsnPydWFgKjvh3J5hh' );
    $registrationIds = "dTDs6rF7h04:APA91bECmREiN_qgUJ2IBwagJRzWmVxDMWVpmjEnkgJi1FWMLAtFHGTSYVXA-0VTHMf-42mZH8qLBEdKHFs9Z6oCL1JUf0uJ232z26XJdpEuwMvVPFxunwAZPN9E4-6qKImQEb7b9iHw";
    #prep the bundle
    $msg = array
    (
     'body'  => 'Body  Of Notification',
     'title' => 'Title Of Notification'
     );
    //'notification' - 只會在app顯示時,執行app的接收訊息Service
    //'data'         - 無論app在什麼狀態皆會執行app的接收訊息Service
    $fields = array
    (
     'to'  => $registrationIds,
     'data'     => $msg
     );
    
    
    $headers = array
    (
     'Authorization: key=' . API_ACCESS_KEY,
     'Content-Type: application/json'
     );
    #Send Reponse To FireBase Server
    $ch = curl_init();
    curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
    curl_setopt( $ch,CURLOPT_POST, true );
    curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
    curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
    $result = curl_exec($ch );
    curl_close( $ch );
    #Echo Result Of FireBase Server
    echo $result;




檔案下載:
參考資料: