java - 获取 "success"而没有收到任何 GCM 消息

标签 java android google-cloud-messaging

我尝试从服务器向手机上的应用发送通知,结果“成功”,但我的手机没有收到任何消息,我正在使用 Google 云消息

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="签名"/>

<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>

====================================================

最佳答案

问题“...没有收到任何东西...”可能是由代码引起的 如下:

if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){... ...

这意味着如果你的设备低于LOLLIPOP,那么你可以接收任何东西, 如果您的设备高于LOLLIPOP,那么您将无法接收任何内容。请检查您所使用手机的Android版本。

关于java - 获取 "success"而没有收到任何 GCM 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35000568/

相关文章:

android - 关于安卓 :configChanges

Android react-native-push-notification onNotification 当应用程序在后台并单击通知时未调用?

push-notification - 推送通知服务器

java - 如何以 ISO 格式返回 Date 对象而不是 Java 中的时间戳?

java - 将 Scala Slick 对象映射到 Java POJO 或从 Java POJO 映射

java - Mapstruct 多对一映射

java - 如何通过 JNI 获取普通的一维数组而不是二维数组?

android - 谷歌地图 onResume

android - 膨胀自定义 View android问题

android - 由于 GCM SenderId Android 无法生成 APK 版本