c# - Trying to send push notifications using GCM from a windows service to IOS devices -
hello
i share issue i'm facing right push notification, have 2 mobile applications 1 android , other ios. need send push notification both depend on database information. android app used following code:
public string sendnotification(string deviceid, string message) { string googleappid = "aizasybvdvwxzvy38vyzn8m67yasqwk27ew7py8"; var sender_id = "305958970287"; var value = message; webrequest trequest; trequest = webrequest.create("https://android.googleapis.com/gcm/send"); trequest.method = "post"; trequest.contenttype = " application/x-www-form-urlencoded;charset=utf-8"; trequest.headers.add(string.format("authorization: key={0}", googleappid)); trequest.headers.add(string.format("sender: id={0}", sender_id)); string postdata = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + value + "&data.time=" + system.datetime.now.tostring() + "®istration_id=" + deviceid + ""; console.writeline(postdata); byte[] bytearray = encoding.utf8.getbytes(postdata); trequest.contentlength = bytearray.length; stream datastream = trequest.getrequeststream(); datastream.write(bytearray, 0, bytearray.length); datastream.close(); webresponse tresponse = trequest.getresponse(); datastream = tresponse.getresponsestream(); streamreader treader = new streamreader(datastream); string sresponsefromserver = treader.readtoend(); treader.close(); datastream.close(); tresponse.close(); return sresponsefromserver; }
this code works fine, received push notifications in devices.
to use gcm ios app follow steps describe in:
gcm documentationthe code send notifications same android app, change api key , sender parameter of course.
the problem ios app don't receive notifications, when run windows service got streamreader treader:
"id=0:1453902236602583%46569b4366d6cf16"
. don't exception in code, seems ok doesn't work. note: don't error in code , exception.
in ios app registration token , need, suppose configuration process correct.
ios code:
// [start_exclude] // configure google context: parses googleservice-info.plist, , initializes // services have entries in file var configureerror:nserror? gglcontext.sharedinstance().configurewitherror(&configureerror) assert(configureerror == nil, "error configuring google services: \(configureerror)") gcmsenderid = gglcontext.sharedinstance().configuration.gcmsenderid print(gcmsenderid!) // [end_exclude] // register remote notifications let settings: uiusernotificationsettings = uiusernotificationsettings(fortypes: [.alert, .badge, .sound], categories: nil) application.registerusernotificationsettings(settings) application.registerforremotenotifications() // [end register_for_remote_notifications] // [start start_gcm_service] let gcmconfig = gcmconfig.defaultconfig() gcmconfig.receiverdelegate = self gcmservice.sharedinstance().startwithconfig(gcmconfig) // [end start_gcm_service]
getting registration token:
func registrationhandler(registrationtoken: string!, error: nserror!) { if (registrationtoken != nil) { self.registrationtoken = registrationtoken let preferences = nsuserdefaults.standarduserdefaults() let forkeypath = session_data + "_notificationtoken" _ = preferences.setobject(registrationtoken, forkey: forkeypath) _ = preferences.synchronize() print("registration token: \(registrationtoken)") self.subscribetotopic() let userinfo = ["registrationtoken": registrationtoken] nsnotificationcenter.defaultcenter().postnotificationname( self.registrationkey, object: nil, userinfo: userinfo) } else { print("registration gcm failed error: \(error.localizeddescription)") let userinfo = ["error": error.localizeddescription] nsnotificationcenter.defaultcenter().postnotificationname( self.registrationkey, object: nil, userinfo: userinfo) } }
apn test result:
2016-02-02 15:07:43 +0000: loaded document aps_development.cer 2016-02-02 15:07:50 +0000: connected server gateway.sandbox.push.apple.com 2016-02-02 15:07:50 +0000: set ssl connection 2016-02-02 15:07:50 +0000: set peer domain name gateway.sandbox.push.apple.com 2016-02-02 15:07:50 +0000: keychain opened 2016-02-02 15:07:50 +0000: certificate data apple development ios push services: com.jperera.rapidsentrymaster initialized 2016-02-02 15:07:50 +0000: sec identity created 2016-02-02 15:07:50 +0000: client certificate created 2016-02-02 15:07:58 +0000: connected 2016-02-02 15:07:58 +0000: token: <00000001 0000000c 000001bf bf010000 000001bf bf010000 000001bf bf010000 000001bf bf010000 000001bf bf010000 000001bf 000000d8 d8000000 000000d8 0000000f 0f000000 0000000f> 2016-02-02 15:07:58 +0000: written 92 bytes sending data gateway.sandbox.push.apple.com:2195 2016-02-02 15:07:58 +0000: disconnected server gateway.sandbox.push.apple.com:2195
i appreciate on that
aha! response looks familiar me.
when call gcm http post
send json, json response back... {"multicast_id":5557nnn163626712336,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1451239821391315%e0bf69abc9fd7ecd"}]}
...and last part looks same response get.
i believe gcm telling went ok , message id
can use in gcm diagnostics tool if want (part of google play developer console). headers web request, telling multicast_id
or number of success/failures?
this tool might reveal happened message. isn't perfect can help. maybe gcm thinks message sent ok, failed later on, whilst in hands of apple, before reaching ios device?
one thing may want watch out gcm has 2 ways configure payload (what json, , &data.message=" + value
) , might want try using notification
instead. see 'payload' section @ https://developers.google.com/cloud-messaging/concept-options confess not understand yet!
Comments
Post a Comment