java - I got message id from gcm. but I'm not receiving on emulator -



java - I got message id from gcm. but I'm not receiving on emulator -

this sender code used send message gcm.

//sjsonobject obj = new jsonobject(); sender sender = new sender(google_server_key); message message = new message.builder().timetolive(120) .delaywhileidle(true).adddata(message_key, usermessage).build(); system.out.println("regid: " + regid); object result = sender.send(message, regid, 5); if (stringutils.isempty(((result) result).geterrorcodename())) { system.out.println("success"); system.out.println(result.tostring()); } else{ system.out.println(((result) result).geterrorcodename()); system.out.println(result.tostring()); }

manifest file:

<uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.get_accounts" /> <uses-permission android:name="android.permission.wake_lock" /> <permission android:name="com.javapapers.android.permission.c2d_message" android:protectionlevel="signature" /> <uses-permission android:name="com.javapapers.android.permission.c2d_message" /> <uses-permission android:name="com.google.android.c2dm.permission.receive" /> <uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.vibrate" /> <uses-sdk android:minsdkversion="9" android:targetsdkversion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".registeractivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name="com.javapapers.android.mainactivity" android:configchanges="orientation|keyboardhidden" android:label="@string/app_name" > </activity> <receiver android:name=".gcmbroadcastreceiver" android:permission="com.google.android.c2dm.permission.send" > <intent-filter> <action android:name="com.google.android.c2dm.intent.receive" /> <action android:name="com.google.android.c2dm.intent.registration" /> <category android:name="com.javapapers.android" /> </intent-filter> </receiver> <service android:name=".gcmnotificationintentservice" /> <!-- added shashank --> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> </application>

ccmbroadcastreciever:

package com.javapapers.android; import android.app.activity; import android.content.componentname; import android.content.context; import android.content.intent; import android.support.v4.content.wakefulbroadcastreceiver; import android.util.log; public class gcmbroadcastreceiver extends wakefulbroadcastreceiver { @override public void onreceive(context context, intent intent) { log.d("gcmbroadcastreceiver", "onreceive method"); system.out.println( "broadcast receiver onreceive method"); componentname comp = new componentname(context.getpackagename(), gcmnotificationintentservice.class.getname()); startwakefulservice(context, (intent.setcomponent(comp))); setresultcode(activity.result_ok); } }

gcmnotificationintentservice:

package com.javapapers.android; import android.app.intentservice; import android.app.notificationmanager; import android.app.pendingintent; import android.content.context; import android.content.intent; import android.os.bundle; import android.os.systemclock; import android.support.v4.app.notificationcompat; import android.util.log; import com.google.android.gms.gcm.googlecloudmessaging; public class gcmnotificationintentservice extends intentservice { public static final int notification_id = 1; private notificationmanager mnotificationmanager; notificationcompat.builder builder; public gcmnotificationintentservice() { super("gcmintentservice"); } public static final string tag = "gcmnotificationintentservice"; @override protected void onhandleintent(intent intent) { log.d("gcmnotificationintentservice", "onhandleintent method"); system.out.println( "gcmnotificationintentservice onhandleintent method"); 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)) { (int = 0; < 3; i++) { log.i(tag, "working... " + (i + 1) + "/5 @ " + systemclock.elapsedrealtime()); seek { thread.sleep(5000); } grab (interruptedexception e) { } } log.i(tag, "completed work @ " + systemclock.elapsedrealtime()); sendnotification("message received google gcm server: " + extras.get(config.message_key)); log.i(tag, "received: " + extras.tostring()); } } gcmbroadcastreceiver.completewakefulintent(intent); } private void sendnotification(string msg) { log.d(tag, "preparing send notification...: " + msg); mnotificationmanager = (notificationmanager) .getsystemservice(context.notification_service); pendingintent contentintent = pendingintent.getactivity(this, 0, new intent(this, mainactivity.class), 0); notificationcompat.builder mbuilder = new notificationcompat.builder( this).setsmallicon(r.drawable.gcm_cloud) .setcontenttitle("gcm notification") .setstyle(new notificationcompat.bigtextstyle().bigtext(msg)) .setcontenttext(msg); mbuilder.setcontentintent(contentintent); mnotificationmanager.notify(notification_id, mbuilder.build()); log.d(tag, "notification sent successfully."); } }

output: success [ messageid=0:1414564158966354%31e4cc17f9fd7ecd ]

still i'm not getting force notification on emulator. have tried on device well. same problem occurs there well.

i had similar problem. solved using classes in this github example noting following:

three 'services' , 1 'receiver' permissions added in github example.

androidmanifest.xml

<uses-permission android:name="android.permission.get_accounts" /> <uses-permission android:name="android.permission.read_profile" /> <uses-permission android:name="android.permission.read_contacts" /> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.wake_lock" /> <uses-permission android:name="com.google.android.c2dm.permission.receive" /> <uses-permission android:name="android.permission.vibrate" /> <permission android:name="<yourpackagename>.permission.c2d_message" android:protectionlevel="signature" /> <uses-permission android:name="<yourpackagename>.permission.c2d_message" /> <service android:name=".utility.mygcmregistrationintentservice" android:exported="false" > </service> <service android:name=".utility.mygcmlistenerservice" android:exported="false" > <intent-filter> actionandroid:name="com.google.android.c2dm.intent.receive" /> </intent-filter</service> <service android:name=".utility.myinstanceidlistenerservice" android:exported="false"> <intent-filter> <action android:name="com.google.android.gms.iid.instanceid"/> </intent-filter> </service> <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" /> <action android:name="com.google.android.c2dm.intent.registration"/> <category android:name="<com.example.yourpackagename>" /> </intent-filter> </receiver>

the receiver implemented in

activitymain.java:

mregistrationbroadcastreceiver = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { sharedpreferences sharedpreferences = preferencemanager.getdefaultsharedpreferences(context); boolean senttoken = sharedpreferences.getboolean(sent_token_to_server, false); if (senttoken) { } else { } } }; @override protected void onresume() { super.onresume(); localbroadcastmanager.getinstance(this).registerreceiver(mregistrationbroadcastreceiver, new intentfilter("registrationcomplete")); } @override protected void onpause() { localbroadcastmanager.getinstance(this).unregisterreceiver(mregistrationbroadcastreceiver); super.onpause(); }

you can utilize other files are:

mygcmlistenerservice.java - 1 can customize notification icon , text.

myinstanceidlistenerservice.java

quickstartpreferences.java

registrationintentservice.java

java google-cloud-messaging sender

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -