gpt4 book ai didi

Android GCM 收到消息总是返回 null

转载 作者:行者123 更新时间:2023-11-29 19:55:44 25 4
gpt4 key购买 nike

我正在尝试在 Android 上进行推送通知。在服务器站点我使用的是 asp。我可以发送和接收通知,但无法处理我想在通知中显示的消息。 Mesaage 始终返回 null。这是我的 list :

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<permission android:name="com.example.pushnotificationexample.permission.C2D_MESSAGE" android:protectionLevel="signature" />

<uses-permission android:name="com.example.pushnotificationexample.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.example.pushnotificationexample.GcmBroadcastReceiver" 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.pushnotificationexample" />
</intent-filter>
</receiver>

<!-- Register Service -->

<service android:name="com.example.pushnotificationexample.GcmIntentService" />
</application>

还有我的 GcmIntentService

@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

String messageType = gcm.getMessageType(intent);
String s=extras.getString("message");
//try to get message but always return null

if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType)) {
sendNotification("Deleted messages on server: "
+ extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
.equals(messageType)) {
sendNotification("Warning: "
+ extras.get("message"));
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}

最佳答案

首先你需要知道这是服务器端还是客户端的问题。您可以在 phpfiddle.com 上运行这样的 fiddle .

<?php


// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'YOUR-API-ACCESS-KEY-GOES-HERE' );


$registrationIds = array("YOUR DEVICE IDS WILL GO HERE" );

// prep the bundle
$msg = array
(
'message' => 'here is a message. message',
'title' => 'This is a title. title',
'subtitle' => 'This is a subtitle. subtitle',
'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
'vibrate' => 1,
'sound' => 1
);

$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);

$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
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( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );

echo $result;
?>

注意:在 google 开发者控制台上创建 API 访问 key 时,您必须使用 0.0.0.0/0 作为 ip 地址。 (用于测试目的)。

如果这能正常工作,那么它可能是恶意的客户端代码。最可能的错误是您正在读取错误的属性“消息”,以检查在 extras.getString("message") 之前插入的键并查看日志:

        Iterator<String> stringIterator = extras.keySet().iterator();
if (stringIterator.hasNext()) {
Log.d("a", "Amount of keys sent by server: '" + extras.keySet().size() + "' Existing key" + stringIterator.next());
}

关于Android GCM 收到消息总是返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36745073/

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