douyu9159 2016-01-20 06:08
浏览 63

该应用程序将不会收到消息并获得GCM的“NotRegistered”响应

My app won't receive any messages from GCM, when generate number and send it by contact activity I receive it in mysql like this #######:###################aWvJRwO98DIipu8lhqrH7CWYSkHDqv_Cn4liO49HMizICLwZ

so I use whole the number in registration_ids, however I get error "NotRegistered" in response and I don't know where is the problem I tested also by testing sites

this page whose response to send message

  <?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;
    }
}



?>

getRegId in Contact class

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

}

}

class to receive the message

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

I added also

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

and

    apply plugin: 'com.android.application'

    .........

dependencies {
    compile "com.google.android.gms:play-services:8.4.0"
}

.........

apply plugin: 'com.google.gms.google-services'
  • 写回答

2条回答 默认 最新

  • douhui3760 2016-01-20 09:41
    关注

    Check Your Browser Key which is you generate is correct in google api developer console and also in your .php file in which you pass browser-key and UDID.

    You can test it online by http://www.androidbegin.com/tutorial/gcm.html

    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致