应用不会收到消息,并通过 GCM 收到“未注册”响应


The app won't receive message and get "NotRegistered" response with GCM

我的应用程序不会收到来自GCM的任何消息,当生成号码并通过联系人活动发送时,我在mysql中收到它,就像这样###:##aWvJRwO98DIipu8lhqrH7CWYSkHDqv_Cn4liO49HMizICLwZ#

所以我在registration_ids中使用了整个数字,但是我在响应中收到错误"NotRegister",我不知道问题出在哪里我也通过测试站点进行了测试

此页面,其响应要发送消息

  <?php
class GSM {
function _construct() {
}
    public function send_notification($registration_ids, $message){
        include_once './config.php';

        $url = 'https://gcm-http.googleapis.com/gcm/send';
        $fileds = array(
            'registration_ids' => $registration_ids,
            'message' => $message, );

        $headers = array ( 
              'Authorization: key=' . GOOGLE_API_KEY,
              'Content-Type: application/json'
            );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fileds));
        $result = curl_exec($ch);
        if ($result === false) {
            die('Cutl failed:' . curl_error($ch));
        }
        curl_close($ch);
        echo $result;
    }
}

?>

联系人类中的获取注册表ID

public void getRegId() {
new AsyncTask<Void, Void, String>() {
    @Override
    protected String doInBackground(Void... params) {
        String msg = "";
        try {
            if (gcm == null) {
                InstanceID instanceID = InstanceID.getInstance(getActivity().getApplicationContext());
                regId = instanceID.getToken(projectNumber, GoogleCloudMessaging.INSTANCE_ID_SCOPE);
                msg = "Device registration, registration ID=" + regId;
                Log.i(TAG, msg);
            }
        } catch (IOException e) {
            e.printStackTrace();
            msg = "Error :" + e.getMessage();
        }
        return msg;
    }
}.execute(null, null, null);
}
}

类来接收消息

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());
    }
}
}

安卓清单.xml

<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" />
.........
<service
            android:name=".GCMIntentService"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>

我还添加了

dependencies {
    classpath 'com.android.tools.build:gradle:2.0.0-alpha3'
    classpath 'com.google.gms:google-services:2.0.0-alpha3'

    apply plugin: 'com.android.application'
    .........
dependencies {
    compile "com.google.android.gms:play-services:8.4.0"
}
.........
apply plugin: 'com.google.gms.google-services'

检查您在 Google API 开发者控制台以及传递浏览器密钥和 UDID 的.php文件中生成的浏览器密钥是否正确。

您可以通过以下方式在线测试http://www.androidbegin.com/tutorial/gcm.html

如果 GCM 响应"notRegister"错误,请确保推送服务器删除存储的注册 ID。尝试使用存储的 regID 发送消息时,服务器将继续收到此错误。

您可能需要检查与"未注册"相关的类似问题

您可以参考此链接以获取有关您收到的错误的更多信息:(https://developers.google.com/cloud-messaging/http-server-ref#error-codes)