<permission android:name="**my_app_package**.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="**my_app_package**.permission.C2D_MESSAGE" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.WAKE_LOCK" />
<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="**my_app_package**" /> </intent-filter> </receiver> <service android:name=".GCMIntentService" />
@com.google.gson.annotations.SerializedName("channel") private String mRegistrationId; public String getRegistrationId() { return mRegistrationId; } public final void setRegistrationId(String registrationId) { mRegistrationId = registrationId; }
import com.google.android.gcm.GCMRegistrar;
private String mRegistationId; public static final String SENDER_ID = "<SENDER_ID>";
GCMRegistrar.checkDevice(this); GCMRegistrar.checkManifest(this); mRegistationId = GCMRegistrar.getRegistrationId(this); if (mRegistationId.equals("")) { GCMRegistrar.register(this, SENDER_ID); }
item.setRegistrationId(mRegistationId.equals("") ? GCMIntentService.getRegistrationId() : mRegistationId);
import android.app.NotificationManager; import android.support.v4.app.NotificationCompat;
private static String sRegistrationId; public static String getRegistrationId() { return sRegistrationId; } public GCMIntentService(){ super(ToDoActivity.SENDER_ID); }
@Override protected void onMessage(Context context, Intent intent) { NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("New todo item!") .setPriority(Notification.PRIORITY_HIGH) .setContentText(intent.getStringExtra("message")); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(0, mBuilder.build()); } @Override protected void onRegistered(Context context, String registrationId) { sRegistrationId = registrationId; }
function insert(item, user, request) { request.execute({ success: function() { // Write to the response and then send the notification in the background request.respond(); push.gcm.send(item.channel, item.text, { success: function(response) { console.log('Push notification sent: ', response); }, error: function(error) { console.log('Error sending push notification: ', error); } }); } }); }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท