我正在尝试在Android上进行推送通知。在服务器站点我使用asp。我可以发送和接收通知,但无法处理我想在通知中显示的消息。 Mesaage始终返回null。这是我的清单:
<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);
}
答案 0 :(得分:2)
首先,您需要知道服务器端或客户端是否存在问题。你可以在phpfiddle.com上运行这样的小提琴。
<?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访问密钥时,必须使用0.0.0.0/0作为IP地址。 (用于测试目的)。
如果这个工作正常,那么可能是客户端代码是恶意的。最可能的错误是您正在读取错误的属性&#34; message&#34;,在extras.getString(&#34; message&#34;)之前检查键插入并查看日志:
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());
}