gpt4 book ai didi

java - 应用程序不会收到消息并通过 GCM 获得 "NotRegistered"响应

转载 作者:行者123 更新时间:2023-11-29 21:36:23 26 4
gpt4 key购买 nike

我的应用程序不会收到来自 GCM 的任何消息,当生成号码并通过联系 Activity 发送它时,我在 mysql 中收到它,如下所示 #######:############ #######aWvJRwO98DIipu8lhqrH7CWYSkHDqv_Cn4liO49HMizICLwZ

所以我使用了registration_ids中的整个数字,但是我收到错误“NotRegistered”作为响应,我不知道问题出在哪里我也通过测试网站进行了测试

此页面响应发送消息

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



?>

Contact 类中的 getRegId

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

}

}
}

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>

我还添加了

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'

最佳答案

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

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

关于java - 应用程序不会收到消息并通过 GCM 获得 "NotRegistered"响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34892706/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com