android - Push Notifications and Php -
i can store registration_id. when sending message sample page,i nothing.
the response have php is.
done{"multicast_id":5441272458021564268,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1454957764463721%e104448af9fd7ecd"}]} as can see canonical_ids object has 0 value. think should have value,indicating message has been send gcm.
please check php code
<?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 * user_info 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; ?> i know push notification run using broadcast receiver , intent service.
so
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); } }
and intent service.
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()); } }
i not sure going on. broadcast receiver custom generates , action , runs intentservice. services message string.
and here 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> thanks.
Comments
Post a Comment