Android编程实现google消息通知功能示例
程序员文章站
2023-11-04 15:59:22
本文实例讲述了android编程实现google消息通知功能。分享给大家供大家参考,具体如下:
1. 定义一个派生于wakefulbroadcastreceiver的类...
本文实例讲述了android编程实现google消息通知功能。分享给大家供大家参考,具体如下:
1. 定义一个派生于wakefulbroadcastreceiver的类
public class gcmbroadcastreceiver extends wakefulbroadcastreceiver { private static final string tag = "gcmbroadcastreceiver"; @override public void onreceive(context context, intent intent) { log.v(tag, "onreceive start"); componentname comp = new componentname(context.getpackagename(), gcmintentservice.class.getname()); startwakefulservice(context, (intent.setcomponent(comp))); setresultcode(activity.result_ok); log.v(tag, "onreceive end"); } }
2. 用于处理broadcast消息的类
public class gcmintentservice extends intentservice { private static final string tag = "gcmintentservice"; public static final int notification_id = 1; private notificationmanager mnotificationmanager; notificationcompat.builder builder; private intent mintent; public gcmintentservice() { super("gcmintentservice"); log.v(tag, "gcmintentservice start"); log.v(tag, "gcmintentservice end"); } @override protected void onhandleintent(intent intent) { log.v(tag, "onhandleintent start"); 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(extras.getstring("from"), "send error", extras.tostring(), "", null, null); } else if (googlecloudmessaging.message_type_deleted .equals(messagetype)) { sendnotification(extras.getstring("from"), "deleted messages on server", extras.tostring(), "", null, null); } else if (googlecloudmessaging.message_type_message .equals(messagetype)) { log.v(tag, "bundle size = " + extras.size()); boolean sendreply = false; string msgid = null; for (string key : extras.keyset()) { if ("gcm.notification.title".equals(key) || "gcm.notification.body".equals(key) || "gcm.notification.click_action".equals(key) || "gcm.notification.ordernumber".equals(key)) { log.v(tag, "key=" + key + " data=" + extras.getstring(key)); } else if ("gcm.notification.messageid".equals(key)) { sendreply = true; msgid = extras.getstring(key); log.v(tag, "key=" + key + " data=" + msgid); } else { log.v(tag, "key=" + key); } } sendnotification(extras.getstring("from"), extras.getstring("gcm.notification.title"), extras.getstring("gcm.notification.body"), extras.getstring("gcm.notification.click_action"), extras.getstring("gcm.notification.ordernumber"), msgid); log.i(tag, "received: " + extras.tostring()); mintent = intent; if (sendreply) { new sendreceivedreply().execute(msgid); } else { gcmbroadcastreceiver.completewakefulintent(intent); } } } else { gcmbroadcastreceiver.completewakefulintent(intent); } log.v(tag, "onhandleintent end"); } private void sendnotification(string from, string title, string msg, string action, string ordernumber, string msgid) { log.v(tag, "sendnotification start"); log.v(tag, "from=" + from + " title=" + title + " msg=" + msg + " action=" + action + " ordernumber=" + ordernumber); mnotificationmanager = (notificationmanager) this .getsystemservice(context.notification_service); notificationcompat.builder mbuilder = new notificationcompat.builder( this).setsmallicon(r.drawable.ic_launcher) .setcontenttitle(title) .setstyle(new notificationcompat.bigtextstyle().bigtext(msg)) .setcontenttext(msg) .setsound(settings.system.default_notification_uri); pendingintent contentintent = pendingintent.getactivity(this, 0, new intent(this, activitysplash.class), 0); mbuilder.setcontentintent(contentintent); if (ordernumber == null) { mnotificationmanager.notify(notification_id, mbuilder.build()); } else { int n = ordernumber.charat(0); string s = string.format(locale.english, "%d%s", n, ordernumber.substring(1)); n = integer.valueof(s); log.v(tag, "notif id=" + n); mnotificationmanager.notify(n, mbuilder.build()); } gregoriancalendar c = new gregoriancalendar(); utildb.msgadd(c.gettimeinmillis(), title, msg, msgid); intent intent = new intent(utilconst.broadcast_message); localbroadcastmanager.getinstance(this).sendbroadcast(intent); log.v(tag, "sendnotification end"); } /*************************************************************/ /************************** asynctask ************************/ /*************************************************************/ private class sendreceivedreply extends asynctask<string, void, void> { @override protected void doinbackground(string... params) { if (params[0] != null) { arraylist<namevaluepair> nvp = new arraylist<namevaluepair>(); nvp.add(new basicnamevaluepair("messageid", params[0])); utilapp.dohttppost(getstring(r.string.url_base) + getstring(r.string.url_msg_send_received), nvp, null); } return null; } @override protected void onpostexecute(void result) { gcmbroadcastreceiver.completewakefulintent(mintent); } } }
服务器端(c#):
public class googlenotificationrequestobj { public ilist<string> registration_ids { get; set; } public dictionary<string, string> notification { get; set; } } private static ilog _log = logmanager.getlogger(typeof(googlenotification)); public static string callgoogleapi(string receiverlist, string title, string message, string messageid = "-1") { string result = ""; string applicationid = configurationmanager.appsettings["gcmauthkey"]; webrequest wrequest; wrequest = webrequest.create("https://gcm-http.googleapis.com/gcm/send"); wrequest.method = "post"; wrequest.contenttype = " application/json;charset=utf-8"; wrequest.headers.add(string.format("authorization: key={0}", applicationid)); string postdata; var obj = new googlenotificationrequestobj() { registration_ids = new list<string>() { receiverlist }, notification = new dictionary<string, string> { {"body", message}, {"title", title} } }; if (messageid != "-1") { obj.notification.add("messageid", messageid); } postdata = jsonconvert.serializeobject(obj); _log.info(postdata); byte[] bytes = encoding.utf8.getbytes(postdata); wrequest.contentlength = bytes.length; stream stream = wrequest.getrequeststream(); stream.write(bytes, 0, bytes.length); stream.close(); webresponse wresponse = wrequest.getresponse(); stream = wresponse.getresponsestream(); streamreader reader = new streamreader(stream); string response = reader.readtoend(); httpwebresponse httpresponse = (httpwebresponse)wresponse; string status = httpresponse.statuscode.tostring(); reader.close(); stream.close(); wresponse.close(); if (status != "ok") { result = string.format("{0} {1}", httpresponse.statuscode, httpresponse.statusdescription); } return result; }
更多关于android相关内容感兴趣的读者可查看本站专题:《android编程之activity操作技巧总结》、《android资源操作技巧汇总》、《android文件操作技巧汇总》、《android开发入门与进阶教程》、《android视图view技巧总结》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。