透過Google的Firebase來實現推播
使用Firebase只要你有Google帳號就可以用囉
Step 1:
https://firebase.google.com/
先到Firebase的網站登入您的Google帳號
登入後會看到這畫面,按下GET STARTED就可以開始啦
Strp 2:
開始新增專案,專案是很重要的可以讓你好好管理你所要推播的對象是誰
輸入你所想要的專案名稱,選擇你所在的國家
Step 3:
這一頁就是你這個專案底下所管理的東西,今天我們要利用的裝置是Android
選擇將Firebase加入您的Android 應用程式
Android 套件名稱要到你的Android Studio裡面,打開app層級的build.gradle找到applicationId
那個就是要填到Android 套件名稱的
Step 4:
按照他的教學下載google-services.json放到app資料夾裡面
記得!!!如果你換了另一個Android Studio的專案,要重新Step 3~4的步驟
不能直接把google-service.json這個東西複製到其他的專案裡面!!
按照他的教學到不同的build.gradle新增
這個是專案層級的
第二步就直接把下面這行貼到最底端就好啦~
apply plugin: 'com.google.gms.google-services'
但是在應用程式階層的這個build.gradle還有東西要新增
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.0-beta5'
compile 'com.google.firebase:firebase-core:11.0.2'
compile 'com.google.firebase:firebase-messaging:11.0.2'
compile 'com.firebase:firebase-jobdispatcher:0.6.0'
testCompile 'junit:junit:4.12'
}
我也不知道為何在剛剛那個頁面他沒有說明
但是記得要新增這兩行,還有記得要把Android Studio的SDK Manager裡面的東西都安裝到最新版
記得貼完之後要按Sync now
Step 5:
新增一個Service我把它命名為MyFiewbaseInstanceIDService
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService{
private static final String TAG = "MyFirebaseIIDService";
@Override
public void onTokenRefresh() {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
// TODO: Implement this method to send token to your app server.
}
}
新增另一個Service命名為MyFirebaseMessagingService
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.i("Service", "onMessageReceived: " + remoteMessage.getFrom());
if (remoteMessage.getData().size() > 0) {
Log.i("Service", "" + remoteMessage.getData());
}
if (remoteMessage.getNotification() != null) {
Log.i("Service", "" + remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getBody());
}
}
@Override
public void onMessageSent(String s) {
super.onMessageSent(s);
Log.i("Service", "onMessageSent" + s);
}
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 /* Request code */, 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("App")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
Step 6:
到AndroidManifest.xml
新增meta-data以及service
<!-- Set custom default icon. This is used when no icon is set for incoming notification messages. See README(https://goo.gl/l4GJaQ) for more. --> <meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/ic_stat_ic_notification" /> <!-- Set color used with incoming notification messages. This is used when no color is set for the incoming notification message. See README(https://goo.gl/6BKBk7) for more. --> <meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@color/colorAccent" />
<service android:name="com.xxxxxx.user.MyFirebaseMessagingService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter> </service>
<service android:name="com.xxxxxx.user.MyFirebaseInstanceIDService"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> </intent-filter> </service>
Step 7:
然後App的部分就差不多了
到Firebase裡面選擇你想要推播的內容
在訊息文字填上你所想要推播給使用者看的內容
然後按下傳送訊息就完成了~~
Step 8:
推播的速度其實算很快~~我按下傳送訊息不到五秒手機這邊就收到通知了
其實步驟看起來很多但是一步一步跟著做其實很簡單的

*****
版主大大抱歉,上面的問題我解決了,我想問另一個問題是如果接收不到firebase發出的推波,我可能是哪裡出現問題呢?
Hi您好 要不要先確認手機有沒有連網呢? 因為沒看到哪邊有bug只能先這樣猜喔!
感謝大大的回覆!我自己搞笑,寫完後忘了去Run,所以手機沒有同步,哈哈,我想問另一個問題:firebase我只有看到訊息的推播,如果要圖片的推播該怎麼做呢?
*****
*****
*****
*****
*****
*****
*****
*****
*****