GCM 未在 android 4.0.4 设备上接收消息
GCM not receiving messages on android 4.0.4 device
我正在尝试让 GCM 在装有 android 4.0.4 的设备上运行。
首先,从设备发送到服务器到另一个设备是没有问题的,是接收部分不行。其次,我还在使用 android 5 的设备,在 phone 上一切正常。我认为这可能与设备有关,但我不确定 (MEDION p4013)。它是 android 4.0.4,因此您不需要 google 帐户即可运行,但可以肯定的是,我还在 phone 上激活了一个帐户,但仍然没有结果。
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<permission android:name="com.example.tom.stapp3.permission.C2D_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.tom.stapp3.permission.C2D_MESSAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name=".application.StApp">
<receiver android:name=".gcm.GCMBroadCastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
<category android:name="com.example.tom.stapp3"/>
</intent-filter>
</receiver>
<service android:name=".gcm.GCMMessageHandler"/>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
<service
android:name=".service.ShimmerService"
android:enabled="true">
</service>
<activity
android:name=".activity.FragmentViewer"
android:label="Stapp 3" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activity.ProfileView"
android:label="@string/title_activity_profile_view">
</activity>
<activity
android:name=".activity.LeaderboardView"
android:label="@string/title_activity_leaderboard_view"
android:launchMode="singleTop">
</activity>
<activity android:name=".activity.StrangerView"
android:label="@string/title_activity_stranger_view">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.tom.stapp3.activity.LeaderboardView" />
</activity>
<activity
android:name=".activity.ConnectionView"
android:label="@string/title_activity_connection_view" >
</activity>
<activity
android:name=".activity.QuestList"
android:label="@string/title_activity_quest_view" >
</activity>
<activity
android:name=".activity.SoloQuestDescription"
android:label="@string/title_activity_quest_description" >
</activity>
<activity
android:name=".activity.GraphView"
android:configChanges="orientation|screenSize"
android:label="@string/title_activity_graph">
</activity>
<activity
android:name=".activity.GCMTestActivity"
android:label="@string/title_activity_internet__connection" >
</activity>
</application>
广播接收器:
@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(), GCMMessageHandler.class.getName());
Log.d("received", "message");
startWakefulService(context, intent.setComponent(comp));
setResultCode(Activity.RESULT_OK);
}
和处理程序:
private Handler handler;
public GCMMessageHandler() {
super("GCMMessageHandler");
}
@Override
public void onCreate() {
super.onCreate();
handler = new Handler();
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
final int challengeId = Integer.parseInt(extras.getString("challenge_id"));
final String message = extras.getString("message");
handler.post(new Runnable() {
@Override
public void run() {
if(challengeId == -1) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
} else {
//TODO implementatie
}
}
});
GCMBroadCastReceiver.completeWakefulIntent(intent);
}
好的,我找到了,
问题出在应用程序的 build.gradle 中。 applicationId 与应用程序的包名称不匹配。显然,对于 android 的旧版本,您会遇到问题,但对于新版本,它会自动将其更改为某处。
我正在尝试让 GCM 在装有 android 4.0.4 的设备上运行。 首先,从设备发送到服务器到另一个设备是没有问题的,是接收部分不行。其次,我还在使用 android 5 的设备,在 phone 上一切正常。我认为这可能与设备有关,但我不确定 (MEDION p4013)。它是 android 4.0.4,因此您不需要 google 帐户即可运行,但可以肯定的是,我还在 phone 上激活了一个帐户,但仍然没有结果。
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<permission android:name="com.example.tom.stapp3.permission.C2D_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.tom.stapp3.permission.C2D_MESSAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name=".application.StApp">
<receiver android:name=".gcm.GCMBroadCastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
<category android:name="com.example.tom.stapp3"/>
</intent-filter>
</receiver>
<service android:name=".gcm.GCMMessageHandler"/>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
<service
android:name=".service.ShimmerService"
android:enabled="true">
</service>
<activity
android:name=".activity.FragmentViewer"
android:label="Stapp 3" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activity.ProfileView"
android:label="@string/title_activity_profile_view">
</activity>
<activity
android:name=".activity.LeaderboardView"
android:label="@string/title_activity_leaderboard_view"
android:launchMode="singleTop">
</activity>
<activity android:name=".activity.StrangerView"
android:label="@string/title_activity_stranger_view">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.tom.stapp3.activity.LeaderboardView" />
</activity>
<activity
android:name=".activity.ConnectionView"
android:label="@string/title_activity_connection_view" >
</activity>
<activity
android:name=".activity.QuestList"
android:label="@string/title_activity_quest_view" >
</activity>
<activity
android:name=".activity.SoloQuestDescription"
android:label="@string/title_activity_quest_description" >
</activity>
<activity
android:name=".activity.GraphView"
android:configChanges="orientation|screenSize"
android:label="@string/title_activity_graph">
</activity>
<activity
android:name=".activity.GCMTestActivity"
android:label="@string/title_activity_internet__connection" >
</activity>
</application>
广播接收器:
@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(), GCMMessageHandler.class.getName());
Log.d("received", "message");
startWakefulService(context, intent.setComponent(comp));
setResultCode(Activity.RESULT_OK);
}
和处理程序:
private Handler handler;
public GCMMessageHandler() {
super("GCMMessageHandler");
}
@Override
public void onCreate() {
super.onCreate();
handler = new Handler();
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
final int challengeId = Integer.parseInt(extras.getString("challenge_id"));
final String message = extras.getString("message");
handler.post(new Runnable() {
@Override
public void run() {
if(challengeId == -1) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
} else {
//TODO implementatie
}
}
});
GCMBroadCastReceiver.completeWakefulIntent(intent);
}
好的,我找到了,
问题出在应用程序的 build.gradle 中。 applicationId 与应用程序的包名称不匹配。显然,对于 android 的旧版本,您会遇到问题,但对于新版本,它会自动将其更改为某处。