【发布时间】:2016-01-29 19:27:51
【问题描述】:
我尝试从服务器向我手机上的应用发送通知,我得到“成功”,但我的手机没有收到任何消息,我正在使用 Google Cloud Messaging,尽管我收到了带有消息的实例 ID我的服务器并使用它来发送消息。
public class GCMIntentService extends GcmListenerService{
private static final String TAG = "GCMIntentService";
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
Log.d(TAG, "from:" + from);
Log.d(TAG, "message:" + message);
sendNotification(message);
}
private void sendNotification(String message){
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo)
.setContentTitle("New Message")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSound)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
}
AndroidManifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission android:name="com.example.abdul_majeed.alruthea.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.example.abdul_majeed.alruthea.permission.C2D_MESSAGE" />
....
<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="com.example.abdul_majeed.alruthea" />
</intent-filter>
</receiver>
<service
android:name=".GCMIntentService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
========================================
【问题讨论】:
-
GCM 消息是否成功传送到您的设备?您的 adb 日志是否显示任何有关 "from: xxx" "message: yyy" 的消息?
-
您好,请考虑添加服务器端代码。您是仅检查来自 GCM 服务器的 http 代码,还是同时检查有效负载以了解成功设备的数量?
-
您还在使用 C2DM(Android 云到设备消息传递)吗?根据您的清单文件,您似乎仍在使用旧版本。 C2DM 已于 2012 年 6 月 26 日正式弃用,服务于 2015 年 10 月 20 日完全关闭。C2DM 不接受新用户,也不授予新配额。强烈建议 C2DM 开发人员迁移到 Google Cloud Messaging (GCM)。 GCM 是 C2DM 的下一代。这是关于如何从 C2DM 迁移到 GCM 的 Google 官方文档:developers.google.com/cloud-messaging/c2dm
标签: java android google-cloud-messaging android-manifest