public class gcmintentservice extends intentservice { public static final int notification_id = 1; private static final string tag = "gcmintentservice"; private notificationmanager mnotificationmanager; notificationcompat.builder builder; public gcmintentservice() { super("gcmintentservice"); } @override protected void onhandleintent(intent intent) { bundle extras = intent.getextras(); googlecloudmessaging gcm = googlecloudmessaging.getinstance(this); // getmessagetype() intent parameter must intent received // in broadcastreceiver. string messagetype = gcm.getmessagetype(intent); if (!extras.isempty()) { // has effect of unparcelling bundle /* * filter messages based on message type. since gcm * extended in future new message types, ignore * message types you're not interested in, or don't * recognize. */ 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()); // if it's regular gcm message, work. } else if (googlecloudmessaging. message_type_message.equals(messagetype)) { // loop represents service doing work. (int i=0; i<5; i++) { log.i(tag, "working... " + (i+1) + "/5 @ " + systemclock.elapsedrealtime()); try { thread.sleep(5000); } catch (interruptedexception e) { } } log.i(tag, "completed work @ " + systemclock.elapsedrealtime()); // post notification of received message. sendnotification(extras.getstring("notice")); log.i(tag, "received: " + extras.tostring()); } } // release wake lock provided wakefulbroadcastreceiver. gcmbroadcastreceiver.completewakefulintent(intent); } // put message notification , post it. // 1 simple example of might choose // gcm message. private void sendnotification(string msg) { mnotificationmanager = (notificationmanager) this.getsystemservice(context.notification_service); pendingintent contentintent = pendingintent.getactivity(this, 0, new intent(this, notification.class),pendingintent.flag_update_current ); //pendingintent contentintent = pendingintent.getactivity(getapplicationcontext(), //0, contentintent, pendingintent.flag_update_current); notificationcompat.builder mbuilder = new notificationcompat.builder(this) // .setsmallicon(r.drawable.ic_stat_gcm) .setcontenttitle("mobilehealth") .setsmallicon(r.drawable.ic_launcher) .setstyle(new notificationcompat.bigtextstyle() .bigtext(msg)) .setcontenttext(msg); mbuilder.setcontentintent(contentintent); mnotificationmanager.notify(notification_id, mbuilder.build()); }
}
here code notification. able message using ip address when have first send message when using new ip address not able message , in log cat displays invalid app , invalid package name : perhaps didnot include pendingintent in extras
Comments
Post a Comment