甚麼是GCM
server端可以藉由此通知client端一些消息,例如: line的訊息通知,軟體更新通知,或者某些商品的特賣通知。
如何實現GCM
首先必須先寫出client端程式,client端必須註冊GCM,並且設定一個receiver去接收GCM
以下是範例
GCMRegistrar.checkDevice(this); GCMRegistrar.checkManifest(this); final String regId = GCMRegistrar.getRegistrationId(this); Log.d("Ted", "regId = " + regId); if (regId.equals("")) { GCMRegistrar.register(this, Project_ID); } else { Log.d("Ted", "Already registered"); }
上面幾行會註冊GCM,接著在manifest裡面加入以下的程式
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.VIBRATE" /> <permission android:name="com.example.momoandroid.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="com.example.momoandroid.permission.C2D_MESSAGE" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.gcmsample.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <category android:name="com.example.gcmsample" /> </intent-filter> </receiver> <service android:name=".GCMIntentService" /> </application>
使用receiver接收gcm,並且設定service為 GCMIntentService
然後GCMIntentService繼承 GCMBaseIntentService
接著在onMessage也就是收到訊息時做相關的動作
NotificationManager notificationManager = (NotificationManager) context .getSystemService(NOTIFICATION_SERVICE); /* * Intent notifyIntent = new Intent(context ,MainActivity.class); * notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent * appIntent = PendingIntent.getActivity(context,0, notifyIntent, 0); */ Notification notification = new Notification(); notification.icon = R.drawable.ic_launcher; notification.tickerText = "notification on status bar."; notification.defaults = Notification.DEFAULT_ALL; notification.setLatestEventInfo(context, "Title", message, null); notificationManager.notify(0, notification);
如上面的程式碼,這樣就可以在收到訊息時產生推撥
然後在 OnRegiter時將 Register_Id記起來之後在server端會使用到
上面的程式碼有個Project_ID必須去google developer console申請
首先去 google console 開啟一個project https://cloud.google.com/console/project
接著就可以在overview欄位找到Project Number ,也就是我們需要的Project_ID
接著開啟 google cloud messaging for android
接著create key create browser key
接著將API key 記起來 之後在server端需要使用
目前我們已經取得了 server 端需要的API_Key 以及 Register_Id
接著我們只需要開創一個java project
然後key入以下程式碼
Sender sender = new Sender(API_Key );
ArrayList<String> devicesList = new ArrayList<String>();
String device =Register_Id;
devicesList.add(device);
Message message = new Message.Builder()
//.collapseKey("message")
//.timeToLive(241000)
.delayWhileIdle(true)
.addData("message", "Your message send")
.build();
MulticastResult result = sender.send(message, devicesList, 1);
System.out.println(result.toString());
if (result.getResults() != null) {
int canonicalRegId = result.getCanonicalIds();
if (canonicalRegId != 0) {
}
} else {
int error = result.getFailure();
System.out.println(error);
}
當執行完java project 訊息就會被送出,然後client端就會出現如下圖的推撥
在這邊附上 sample code
附註 java project 必須 import一些jar檔 檔案也會附在sample code裡
GCMSample為 client端sendMessage為 server端
沒有留言:
張貼留言