我可以成功存储registration_id。但是当我从我的示例页面发送消息时,我什么都没得到。
我从php得到的回应是。
done{"multicast_id":5441272458021564268,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1454957764463721%e104448af9fd7ecd"}]}
如您所见, canonical_ids 对象的值为零。我认为这应该有一些价值,表明我的消息已发送给GCM。
请检查我的 php代码
<?php
include("init.php");
$url = 'https://gcm-http.googleapis.com/gcm/send';
$apikey = 'AIzaSyDpdrSDJriqAsqCzB7gGURmjrZIm6q0KZM';
$message="";
$registration_ids = array();
if(isset($_POST['email'])){
$name = $_POST['email'];
$message = $_POST['message'];
$sql = "SELECT * FROM user_info where email = '".$name."'";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)){
array_push($registration_ids,$row['token_id']);
}
}
$message_send = array("Notice"=>$message);
$fields = array(
'registration_ids' => $registration_ids,
'data' => $message_send
);
$headers = array('Authorization: key='.$apikey,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
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);
if($result === FALSE){
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
echo "done".$result;
?>
我知道推送通知是使用广播接收器和意图服务运行的。
所以
public class MyGcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(),GcmIntentService.class.getName());
startWakefulService(context,(intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
和意向服务。
public class GcmIntentService extends IntentService{
public static final int NOTIFICATION_ID = 1;
private static final String TAG="GcmIntentService";
private NotificationManager mNotificationManager;
NotificationCompat.Builder mBuilder;
public GcmIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
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(extras.getString("Notice"));
Log.v(TAG,"Received " + extras.toString());
}
MyGcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String s) {
Log.v(TAG,s);
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this,0,
new Intent(this,MainActivity.class),0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setContentTitle("GCMDemo")
.setSmallIcon(R.mipmap.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle().bigText(s))
.setContentText(s);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID,mBuilder.build());
}
}
我不确定到底发生了什么。广播接收器是自定义生成和动作并运行IntentService。此服务将消息作为额外的字符串获取。
最后这是我的 manifest.xml 。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="testing.theo.testpushnotifications">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="testing.theo.testpushnotifications.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="testing.theo.testpushnotifications.permission.C2D_MESSAGE" />
<application
android:name=".AppController"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<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=".MyGcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
<category android:name="testing.theo.testpushnotifications"/>
</intent-filter>
</receiver>
<service android:name=".GcmIntentService"/>
<activity android:name=".Register"></activity>
</application>
感谢。