●補充說明--推播機制

1.不分android或ios,推播是推給系統,是GOOGLE 推給系統,先把要推播的內容給APP的SERVER,APP的SERVER會告訴GOOGLE SERVER要推給誰,GOOGLER SERVER再推給系統,再把APP叫出來

2.推播一定要有server,我們這邊用firebase,原理和運作方式如下
=>當安裝好app(ex:line)的時候會註冊帳號,會跟google說要一個推播token,要註冊這隻手機需要一個推播token,google就會回傳token,app會收到token
=>把token加上user id 傳到line 的server去,line要用資料庫存下來
=>以後要發訊息的時候,就要用token+訊息+app金鑰(key=>在google developer console申請的)
=>看到key就會知道是哪個app,看到token就會知道是哪支手機,這樣google就會送訊息過來
**同一支手機,不同app,token會一樣,所以token是綁手機
 
3.用firebase console做發送就沒有token問題,送推播就是一次送所有的人

 

●程式說明:

1.tools=>firebase
FCM firebase cloud messaging
 
2.cloud messaging
FCM firebase cloud messaging
 
3.SET UP FIREBASE CLOUD MESSAGING=>CONNECT
FCM firebase cloud messaging
 
連結時會跳出選擇授權goolge帳號,再回android studio設定Firebase project name
 
FCM firebase cloud messaging
 
完成後右下角有出現這個訊息
FCM firebase cloud messaging
 
 
4.點Add FCM to your app. FCM(firebase cloud message)的library=>加入gradle並且sync
FCM firebase cloud messaging
 
5.app每次啟動時跟goolge要一個token,並且註冊到firebase
=>先new一個service:myfirebaseService
FCM firebase cloud messaging
 
6.繼承FirebaseMessagingService,下面IBinder可以刪除
public class MyFirebaseService extends FirebaseMessagingService{
public MyFirebaseService() {
}
}
 
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
 
7.加入onMessageReceived
public class MyFirebaseService extends FirebaseInstanceIdService {
public MyFirebaseService() {
}
 
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // ...
 
 
    // TODO(developer): Handle FCM messages here.
    // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
    Log.d(TAG, "From: " + remoteMessage.getFrom());
 
 
    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
 
 
    }
 
 
    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }
 
 
    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
}
}
 
8.manifest加intent filter(已可收推播)
<intent-filter>
    <action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
 
 
 
9.先離開app,到firebase找到該專案=>左側Cloud Messaging
FCM firebase cloud messaging
 
11.輸入訊息=>審查=>發布
FCM firebase cloud messaging
FCM firebase cloud messaging
 
12.在手機收到
FCM firebase cloud messaging
 
13.完成畫面
FCM firebase cloud messaging

 

●程式參考(GitHub):使用Google Firebase Cloud Messaging接收推播訊息 

arrow
arrow
    創作者介紹
    創作者 muchone 的頭像
    muchone

    簡單。生活。享受

    muchone 發表在 痞客邦 留言(0) 人氣()