我正在使用此Android hive 获取推送通知的教程。
我无法从gcm获取注册ID,因为GCMIntentService 在mainActivity中注册id变为null后,class运行。
执行mainActivity.After时,注册ID始终为null 有时只有我从gcmintentService获得注册ID。
我正在使用root包中的所有gcm代码。
下面我发布了与此相关的代码。
MainActivity.java:
GCMRegistrar.checkDevice(MainActivity.this);
GCMRegistrar.checkManifest(MainActivity.this);
lblMessage = (TextView) findViewById(R.id.lblMessage);
registerReceiver(mHandleMessageReceiver,new IntentFilter(DISPLAY_MESSAGE_ACTION));
Log.e("mHandleMessageReceiver", ""+mHandleMessageReceiver);
// Get GCM registration id
final String regId = GCMRegistrar.getRegistrationId(MainActivity.this);
Log.e("CheckRegId", ""+regId);
// Check if regid already presents
if (regId.equals("")) {
// Registration is not present, register now with GCM
GCMRegistrar.register(MainActivity.this, SENDER_ID);
Log.e("RegIDNull", "RegIdNull");
} else {
// Device is already registered on GCM
if (GCMRegistrar.isRegisteredOnServer(MainActivity.this)) {
Toast.makeText(getApplicationContext(), "Already registered with GCM", Toast.LENGTH_LONG).show();
} else {
Log.e("HitAynscReg", "HitAynscReg");
mRegisterTask = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
ServerUtilities.register(MainActivity.this, appVersionNameStr,regId, deviceUserId, getDeviceNameStr, "Android", androidVersionNameBuilder.toString(),
String.valueOf(latitude), String.valueOf(longitude), address+", "+areaName+", "+city, userid );
return null;
}
@Override
protected void onPostExecute(Void result) {
mRegisterTask = null;
}
};
mRegisterTask.execute(null, null, null);
}
}
清单:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- Creates a custom permission so only this app can receive its messages. -->
<permission
android:name="com.app.steve.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.app.steve.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- Network State Permissions to detect Internet status -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Permission to vibrate -->
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:name="com.golive.entertainment.volley.AppController"
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@drawable/client_logo"
android:label="@string/app_name"
android:largeHeap="true"
android:theme="@style/AppTheme" >
.......
.......
<activity
android:name="com.app.steve.MainActivity"
android:screenOrientation="portrait" >
</activity>
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.app.steve" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
</application>
GCMIntentService.java:
public class GCMIntentService extends GCMBaseIntentService {
private static final String TAG = "GCMIntentService";
public GCMIntentService() {
super(SENDER_ID);
Log.e("GCMIntentServiceConstructorCalling", "Test");
}
/**
* Method called on device registered
**/
@Override
protected void onRegistered(Context context, String registrationId) {
Log.e("GCMIntentServiceRegID", ""+registrationId);
displayMessage(context, "Your device registred with GCM");
ServerUtilities.register(GCMIntentService.this, MainActivity.appVersionNameStr,registrationId, MainActivity.deviceUserId, MainActivity.getDeviceNameStr, "Android", MainActivity.androidVersionNameBuilder.toString(),
String.valueOf(MainActivity.latitude), String.valueOf(MainActivity.longitude), MainActivity.address+", "+MainActivity.areaName+", "+MainActivity.city, MainActivity.userid );
}
如果我在mainACtivity中的注册ID代码之前运行GCMIntentService,我可以得到预期的结果。任何人都知道如何解决这个问题。
答案 0 :(得分:1)
将此添加到您的清单。
<!-- [START gcm_receiver] -->
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="gcm.play.android.samples.com.gcmquickstart" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
</intent-filter>
</receiver>
<!-- [END gcm_receiver] -->
<!-- [START gcm_listener] -->
<service
android:name=".utils.gcm.ListenerServiceGCM"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<!-- [END gcm_listener] -->
<!-- [START instanceId_listener] -->
<service
android:name=".utils.gcm.InstanceIDListenerServiceGCM"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
<!-- [END instanceId_listener] -->
<service
android:name=".utils.gcm.RegistrationIntentServiceGCM"
android:exported="false">
</service>
<service android:name=".notifications.NotifyService" />
<receiver
android:name=".notifications.OrderResponseReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.notif.test.action_accept" />
<action android:name="com.notif.test.action_reject" />
</intent-filter>
</receiver>
答案 1 :(得分:1)
我通过在SplashScreenActivity.java类中添加以下行解决了这个问题:
GCMRegistrar.register(MainActivity.this, SENDER_ID);
然后点击登录按钮后,我在MainActivity.java中运行GCM代码。所以我可以获得注册ID。因为我们可以花些时间从gcm获得注册ID。
答案 2 :(得分:0)
您可以使用this tutorial。
Google提供了Instance ID API来处理注册令牌的创建和更新。要使用此API,请在清单中包含InstanceIDListenerService:
<service android:name="[.MyInstanceIDService]" android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
要获取令牌,请调用instanceID.getToken,提供应用服务器的发件人ID并将范围设置为GoogleCloudMessaging.INSTANCE_ID_SCOPE。不要在主线程中调用此方法;相反,使用扩展IntentService的服务,如下所示:
public class RegistrationIntentService extends IntentService {
// ...
@Override
public void onHandleIntent(Intent intent) {
// ...
InstanceID instanceID = InstanceID.getInstance(this);
String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
// ...
}
// ...
}
一旦您收到了注册令牌,请务必将其发送到您的服务器。
答案 3 :(得分:0)
您需要直接在包本身中声明 GCMIntentService 类,例如
com.example.demoapp.GCMIntentService;
如果在任何文件夹中声明GCMIntentService类,您将无法获得注册ID。
答案 4 :(得分:-1)
Here is the implementation which I have done in my project.
GCMBroadcastReciever.java
public class GCMBroadcastReciever extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Attach component of GCMIntentService that will handle the intent in background thread
ComponentName comp = new ComponentName(context.getPackageName(),
GCMIntentServices.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
GCMIntentService.java
public class GCMIntentServices extends IntentService {
public static final int NOTIFICATION_ID = 1000;
NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public GCMIntentServices() {
super(GCMIntentServices.class.getName());
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
if (!extras.isEmpty()) {
// read extras as sent from server
String message = extras.getString("message");
String serverTime = extras.getString("timestamp");
sendNotification("Message: " + message + "\n" + "Server Time: "
+ serverTime);
}
// Release the wake lock provided by the WakefulBroadcastReceiver.
GCMBroadcastReciever.completeWakefulIntent(intent);
}
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_arrow_up_blue_soft)
.setContentTitle("Notification from GCM")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
private class GCMRegistrationTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(getApplicationContext());
}
try {
regId = gcm.register(GlobalConfig.YOUR_SENDER_ID);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return regId;
}
@Override
protected void onPostExecute(String result) {
if (result != null) {
Log.e("reg", result);
GlobalCommonMethods.saveValueOnUserPrefernces(MainActivity.this, result, "reg_id");
// Toast.makeText(getApplicationContext(), "registered with GCM"+regId,
// Toast.LENGTH_LONG).show();
// reg();
}
}
}
manifest.xml
<receiver
android:name=".helperclass.GCMBroadcastReciever"
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.geeks.schoolmanagement"/>
</intent-filter>
</receiver>
<service android:name=".helperclass.GCMIntentServices"/>
<!-- make sure to add google-play-services_lib from project properties->android->library-->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<!-- For vibrating device -->
<uses-permission android:name="android.permission.VIBRATE"/>
<!-- For receiving GCM messages -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- For protecting GCM messages so that only your app can receive them -->
<permission
android:name="com.geeks.schoolmanagement.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.geeks.schoolmanagement.permission.C2D_MESSAGE" />