我想在android中发送php通知。
请查看代码
private function sendNotification(){
define('API_ACCESS_KEY', '**************');
$registrationIds = array("APA91bGeI8ILESgDzxoMnBRiP43k46CT31UySs0Q04FTHxyUFgBWrNBFKQKBGHwRvqn-vA7GdNRFrRMtmfQtQ9AGV8zvjYPa0nwWe64HTenIIwyIEIvQx-T0hmRpM7ybQvmmblK2GdnI");
$msg = array(
'message' => 'here is a message. message',
'title' => 'This is a title. title'
);
$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_IPRESOLVE, CURL_IPRESOLVE_V4 );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode($fields) );
$result = curl_exec($ch );
curl_close( $ch );
return $result;
}
当我运行此代码时。它没有在移动设备上发送通知。
并给予
{"multicast_id":6523573899048274101,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1456312662566810%bb518b18f9fd7ecd"}]}
答案 0 :(得分:0)
在Android menifest中添加此内容
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
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="gcm.play.android.samples.com.gcmquickstart" />
</intent-filter>
</receiver>
<receiver
android:name=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.hmkcode.android.gcm" />
</intent-filter>
</receiver>
<service
android:name=".GcmMessageHandler"
android:exported="true" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
创建BrodcastReceiver
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmMessageHandler will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmMessageHandler.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
/* Intent i=new Intent(context,DialogActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);*/
}
}
创建服务
public class GcmMessageHandler extends IntentService {
String mes;
String session;
private Handler handle;
String user;
PowerManager pm ;
boolean isScreenOn;
public GcmMessageHandler() {
super("GcmMessageHandler");
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
handler = new Handler();
pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
isScreenOn = pm.isScreenOn();
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
//Replace with Your Key
mes = extras.getString("type");
user=extras.getString("user");
//put Your notification Code
if (isScreenOn == false) {
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "MyLock");
wl.acquire(10000);
PowerManager.WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyCpuLock");
wl_cpu.acquire(10000);
}
Log.i("GCM", "Received : (" + messageType + ") " + extras.getString("user"));
showToast();
GcmBroadcastReceiver.completeWakefulIntent(intent);
}