
public class MyInstanceIDListenerService extends IntentService {private static final String TAG = "RegIntentService";private static final String[] topICS = {"global"};public MyInstanceIDListenerService() { super(TAG);}@OverrIDeprotected voID onHandleIntent(Intent intent) { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); try { // [START register_for_gcm] // Initially this call goes out to the network to retrIEve the token,subsequent calls // are local. // [START get_token] InstanceID instanceID = InstanceID.getInstance(this); String token = instanceID.getToken(getString(R.string.gcm_defaultSenderID),GoogleCloudMessaging.INSTANCE_ID_ScopE,null); // [END get_token] Log.i(TAG,"GCM Registration Token: " + token); // Todo: Implement this method to send any registration to your app's servers. sendRegistrationToServer(token); // Subscribe to topic channels subscribetopics(token); // You should store a boolean that indicates whether the generated token has been // sent to your server. If the boolean is false,send the token to your server,// otherwise your server should have already received the token. sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER,true).apply(); // [END register_for_gcm] } catch (Exception e) { Log.d(TAG,"Failed to complete token refresh",e); // If an exception happens while fetching the new token or updating our registration data // on a third-party server,this ensures that we'll attempt the update at a later time. sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER,false).apply(); } // Notify UI that registration has completed,so the progress indicator can be hIDden. Intent registrationComplete = new Intent(QuickstartPreferences.REGISTRATION_COMPLETE); LocalbroadcastManager.getInstance(this).sendbroadcast(registrationComplete);}/** * Persist registration to third-party servers. * * Modify this method to associate the user's GCM registration token with any server-sIDe account * maintained by your application. * * @param token The new token. */private voID sendRegistrationToServer(String token) { // Add custom implementation,as needed.}/** * Subscribe to any GCM topics of interest,as defined by the topICS constant. * * @param token GCM token * @throws IOException if unable to reach the GCM PubSub service */// [START subscribe_topics]private voID subscribetopics(String token) throws IOException { for (String topic : topICS) { GcmpubSub pubSub = GcmpubSub.getInstance(this); pubSub.subscribe(token,"/topics/" + topic,null); }}// [END subscribe_topics] }
gcm_defaultSenderID和QuickStartPreferences无法解析.为什么?我怎么能克服它?
解决方法 gcm_defaultSenderID是您必须创建的字符串.这是您的应用发件人ID.QuickStartPreferences就是一个例子.如果您想记住令牌已发送到您的服务器,则可以创建一个用于保存首选项键的类(请参阅此行上方的注释).
总结以上是内存溢出为你收集整理的适用于Android的GCM集成全部内容,希望文章能够帮你解决适用于Android的GCM集成所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)