我已经完成了谷歌开发者页面上提到的所有说明,但我是新手推送通知,所以我无法理解我需要为sendregistrationtoserver()函数添加什么代码。而且我想知道我需要做的相应的PHP脚本来获取注册ID和发送推送消息。 提前谢谢。
package com.babajideal.babajideal;
import android.app.IntentService;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.content.LocalBroadcastManager;
import com.google.android.gms.gcm.GcmPubSub;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.google.android.gms.iid.InstanceID;
import java.io.IOException;
public class RegisterationIntentService extends IntentService {
private static final String TAG = "RegIntentService";
private static final String[] TOPICS = {"global"};
public RegisterationIntentService() {
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
InstanceID instanceID = InstanceID.getInstance(this);
try {
String token = instanceID.getToken(getResources().getString(R.string.gcm_senderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE,null);
sendRegistrationToServer(token);
subscribeTopics(token);
sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, true).apply();
} catch (IOException e) {
e.printStackTrace();
sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false).apply();
}
Intent registrationComplete = new Intent(QuickstartPreferences.REGISTRATION_COMPLETE);
LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}
private void subscribeTopics(String token) throws IOException {
GcmPubSub pubSub = GcmPubSub.getInstance(this);
for (String topic : TOPICS) {
pubSub.subscribe(token, "/topics/" + topic, null);
}
}
private void sendRegistrationToServer(String token) {
// Add custom implementation, as needed.
}
}