Android 未接收Google云消息通知
我正在使用来自服务器的推送通知创建应用程序。我成功地向GCM注册了设备,并获得了注册ID。后来,当我使用ID发送通知时,它没有收到任何通知。我列出了下面所有的代码,我不确定我在哪里出错。请帮帮我。多谢各位Android 未接收Google云消息通知,android,push-notification,google-cloud-messaging,android-notifications,Android,Push Notification,Google Cloud Messaging,Android Notifications,我正在使用来自服务器的推送通知创建应用程序。我成功地向GCM注册了设备,并获得了注册ID。后来,当我使用ID发送通知时,它没有收到任何通知。我列出了下面所有的代码,我不确定我在哪里出错。请帮帮我。多谢各位 <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="22" />
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="22" />
<permission
android:name="com.example.mycab.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.example.mycab.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:name=".AppController"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- Splash screen -->
<activity
android:name=".SplashScreen"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="adjustPan" />
<!-- Main activity -->
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:windowSoftInputMode="adjustPan" />
<activity
android:name=".RegisterActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="adjustPan" />
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<receiver
android:name=".GcmBroadcastReceiver"
android:exported="true"
android:permission="com.google.android.c2m.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.mycab" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
</application>
</manifest>
显示
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="22" />
<permission
android:name="com.example.mycab.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.example.mycab.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:name=".AppController"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- Splash screen -->
<activity
android:name=".SplashScreen"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="adjustPan" />
<!-- Main activity -->
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:windowSoftInputMode="adjustPan" />
<activity
android:name=".RegisterActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="adjustPan" />
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<receiver
android:name=".GcmBroadcastReceiver"
android:exported="true"
android:permission="com.google.android.c2m.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.mycab" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
</application>
</manifest>
gcminentservice.java
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="22" />
<permission
android:name="com.example.mycab.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.example.mycab.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:name=".AppController"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- Splash screen -->
<activity
android:name=".SplashScreen"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="adjustPan" />
<!-- Main activity -->
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:windowSoftInputMode="adjustPan" />
<activity
android:name=".RegisterActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="adjustPan" />
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<receiver
android:name=".GcmBroadcastReceiver"
android:exported="true"
android:permission="com.google.android.c2m.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.mycab" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
</application>
</manifest>
package com.example.mycab;
import com.google.android.gms.gcm.GoogleCloudMessaging;
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;
public class GCMIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public GCMIntentService() {
super("GcmIntentService");
}
public static final String TAG = "GCM Demo";
@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 < 5; 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("Received: " + extras.toString());
Log.i(TAG, "Received: " + extras.toString());
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String 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.ic_home)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
package com.example.mycab;
导入com.google.android.gms.gcm.GoogleCloudMessaging;
导入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;
公共类GCMinentService扩展了IntentService{
公共静态最终整数通知_ID=1;
私人通知经理通知经理;
通知建筑商;
公共GCMinentService(){
超级(“GCMinentService”);
}
公共静态最终字符串TAG=“GCM Demo”;
@凌驾
受保护的手部内容无效(意图){
Bundle extras=intent.getExtras();
GoogleCloudMessaging gcm=GoogleCloudMessaging.getInstance(this);
字符串messageType=gcm.getMessageType(intent);
如果(!extras.isEmpty()){
if(GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)){
sendNotification(“发送错误:+extras.toString());
}else if(GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)){
sendNotification(“服务器上的已删除邮件:+extras.toString());
}else if(GoogleCloudMessaging.MESSAGE\u TYPE\u MESSAGE.equals(messageType)){
对于(int i=0;i<5;i++){
Log.i(标记“正在工作…”+(i+1)
+“/5@”+SystemClock.elapsedRealtime());
试一试{
睡眠(5000);
}捕捉(中断异常e){
}
}
Log.i(标记“Completed work@”+SystemClock.elapsedRealtime());
sendNotification(“已收到:+extras.toString());
Log.i(标记“Received:+extras.toString());
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
私有void sendNotification(字符串msg){
mNotificationManager=(NotificationManager)
this.getSystemService(Context.NOTIFICATION\u服务);
PendingEvent contentIntent=PendingEvent.getActivity(此,0,
新意图(this,MainActivity.class),0);
NotificationCompat.Builder mBuilder=新建NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_home)
.setContentTitle(“GCM通知”)
.setStyle(新通知Compat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID,mBuilder.build());
}
}
发送通知消息后,您的服务器是否从GCM服务器获得成功确认?您可以发布您的服务器代码吗?logcat是否显示设备接收到的任何GCM消息?你的服务器代码是什么?是的,我从GCM得到了成功的确认,Logcat显示它似乎没有任何不工作的地方。我也遵循了下面线程中的步骤。代码已过时,您应该按照官方的快速启动获取代码。您的服务器在发送通知消息后是否从GCM服务器获得成功确认?您可以发布您的服务器代码吗?logcat是否显示设备接收到的任何GCM消息?你的服务器代码是什么?是的,我从GCM得到了成功的确认,Logcat显示它似乎没有任何不工作的地方。我也遵循了下面线程中的步骤。代码已经过时,您应该按照官方的快速入门程序获取代码。