第一次从Android应用程序客户端向GCM服务器注册时收到空通知和空数据,但未向第三方服务器发送id
大家好,我正在通过这个链接关注推送通知教程。我遇到一个问题,所以我想不出如何解决这个问题。我在谷歌上搜索过,但没有找到合适的解决方案。我的问题是,当我第一次正确收到注册id时,我才从android app.client通过项目号注册到gcm服务器,但我的通知显示为null,数据显示为null,我理解reg.id没有发送到第三方服务器,服务器也没有发送数据到gcm,因此,当我第一次注册到gcm服务器时,怎么可能总是显示通知null?我不想第一次启动。请帮助理解这个问题。谢谢 我发布了以下代码:第一次从Android应用程序客户端向GCM服务器注册时收到空通知和空数据,但未向第三方服务器发送id,android,push-notification,google-cloud-messaging,Android,Push Notification,Google Cloud Messaging,大家好,我正在通过这个链接关注推送通知教程。我遇到一个问题,所以我想不出如何解决这个问题。我在谷歌上搜索过,但没有找到合适的解决方案。我的问题是,当我第一次正确收到注册id时,我才从android app.client通过项目号注册到gcm服务器,但我的通知显示为null,数据显示为null,我理解reg.id没有发送到第三方服务器,服务器也没有发送数据到gcm,因此,当我第一次注册到gcm服务器时,怎么可能总是显示通知null?我不想第一次启动。请帮助理解这个问题。谢谢 我发布了以下代码: G
GcmBroadcastReceiver.java
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(),
GCMNotificationIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
GcmBroadcastReceiver.java:-
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(),
GCMNotificationIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
GCMNotificationIntentService.java:-
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.android.gms.gcm.GoogleCloudMessaging;
public class GCMNotificationIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public GCMNotificationIntentService() {
super("GcmIntentService");
}
public static final String TAG = "GCMNotificationIntentService";
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType)) {
sendNotification("Deleted messages on server: "
+ extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
.equals(messageType)) {
for (int i = 0; i < 3; i++) {
Log.i(TAG,
"Working... " + (i + 1) + "/5 @ "
+ SystemClock.elapsedRealtime());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
}
Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
sendNotification("Message Received from Google GCM Server: "
+ extras.get(Config.MESSAGE_KEY));
Log.i(TAG, "Received: " + extras.toString());
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg) {
Log.d(TAG, "Preparing to send notification...: " + msg);
mNotificationManager = (NotificationManager) this
.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.gcm_cloud)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Log.d(TAG, "Notification sent successfully.");
}
}
导入android.app.Activity;
导入android.content.ComponentName;
导入android.content.Context;
导入android.content.Intent;
导入android.support.v4.content.WakefulBroadcastReceiver;
公共类GcmBroadcastReceiver扩展WakefulBroadcastReceiver{
@凌驾
公共void onReceive(上下文、意图){
ComponentName comp=新的ComponentName(context.getPackageName(),
gcmnotificationtentservice.class.getName());
startWakefulService(上下文,(intent.setComponent(comp));
setResultCode(Activity.RESULT\u OK);
}
}
gcmnotificationtentservice.java:-
导入android.app.IntentService;
导入android.app.NotificationManager;
导入android.app.pendingent;
导入android.content.Context;
导入android.content.Intent;
导入android.os.Bundle;
导入android.os.SystemClock;
导入android.support.v4.app.NotificationCompat;
导入android.util.Log;
导入com.google.android.gms.gcm.GoogleCloudMessaging;
公共类GCMNotificationEntService扩展了IntentService{
公共静态最终整数通知_ID=1;
私人通知经理通知经理;
通知建筑商;
公共GCMNotificationtentService(){
超级(“GCMinentService”);
}
公共静态最终字符串TAG=“gcmnotificationtentservice”;
@凌驾
受保护的手部内容无效(意图){
Bundle extras=intent.getExtras();
GoogleCloudMessaging gcm=GoogleCloudMessaging.getInstance(this);
字符串messageType=gcm.getMessageType(intent);
如果(!extras.isEmpty()){
如果(GoogleCloudMessaging.MESSAGE)\类型\发送\错误
.equals(messageType)){
sendNotification(“发送错误:+extras.toString());
}else if(GoogleCloudMessaging.MESSAGE\u TYPE\u已删除
.equals(messageType)){
sendNotification(“服务器上已删除的邮件:”
+附加的toString());
}else if(GoogleCloudMessaging.MESSAGE\u TYPE\u MESSAGE
.equals(messageType)){
对于(int i=0;i<3;i++){
Log.i(标签,
“工作…”+(i+1)+“/5@”
+SystemClock.elapsedRealtime());
试一试{
睡眠(5000);
}捕捉(中断异常e){
}
}
Log.i(标记“Completed work@”+SystemClock.elapsedRealtime());
sendNotification(“从Google GCM服务器收到的消息:”
+extras.get(Config.MESSAGE_KEY));
Log.i(标记“Received:+extras.toString());
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
私有void sendNotification(字符串msg){
Log.d(标记“准备发送通知…”“+msg);
mNotificationManager=(NotificationManager)此
.getSystemService(上下文通知服务);
PendingEvent contentIntent=PendingEvent.getActivity(此,0,
新意图(this,MainActivity.class),0);
NotificationCompat.Builder mBuilder=新建NotificationCompat.Builder(
此).setSmallIcon(R.drawable.gcm_cloud)
.setContentTitle(“GCM通知”)
.setStyle(新通知compat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID,mBuilder.build());
Log.d(标记“通知已成功发送”);
}
}
这一行在您的OnHandleContent
中打印出的内容:Log.i(标记,“Received:+extras.toString())代码>?很明显,它是在那里发出通知的。了解最初消息的内容会很有帮助。我通过从android清单中删除一行来解决我的问题。首先,我定义了这些内容,因此,我删除了-->android:name=“com.google.android.c2dm.intent.REGISTRATION”/>从清单中可以看出,因为这个接收者标签现在已被删除,这就是为什么我在第一次向gcmGood注册时收到空通知和空消息,以得知您已经解决了问题。我还建议查看官方文档以供参考。谢谢您的回答。@Viral感谢您的解决方案。我只是第一次收到gcm通知。我从清单中删除了这行代码,现在可以接收每一个通知。再次感谢!