FCM Downstream message via Android - No notification? -
i try send downstream message via android. response code 200 don't notification. possible send notifications via firebase console, checked already.
this code of post-request:
public class downstreammessage extends asynctask<string,string,string> { asyncresponse delegate = null; int responsecode; @override protected string doinbackground(string... params) { httpurlconnection httpurlconnection = null; bufferedreader bufferedreader = null; string server_key = "key=12345"; string client_key; string content; string content_json_string; try { url url = new url("https://fcm.googleapis.com/fcm/send"); client_key = params[0]; httpurlconnection = (httpurlconnection) url.openconnection(); httpurlconnection.setrequestproperty("content-type", "application/json;charset=utf-8"); httpurlconnection.setrequestproperty("authorization", server_key); httpurlconnection.setconnecttimeout(5000); httpurlconnection.setreadtimeout(10000); httpurlconnection.setrequestmethod("post"); httpurlconnection.connect(); jsonobject notification_json_object = new jsonobject(); try { notification_json_object.put("body","hello world"); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } jsonobject content_json_object = new jsonobject(); try { content_json_object.put("to",client_key); content_json_object.put("notification",notification_json_object); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } content_json_string = content_json_object.tostring(); outputstream output = httpurlconnection.getoutputstream(); output.write(content_json_string.getbytes()); output.flush(); output.close(); responsecode = httpurlconnection.getresponsecode(); } catch (protocolexception e) { } catch (ioexception e) { } return "" + responsecode; } @override protected void onpostexecute(string result) { delegate.processfinish(result); } }
this myfirebasemessagingservice:
public class myfirebasemessagingservice extends firebasemessagingservice { @override public void onmessagereceived(remotemessage remotemessage) { sendnotification(remotemessage); } public void sendnotification(remotemessage remotemessage) { intent intent = new intent(this, mainactivity.class); intent.addflags(intent.flag_activity_clear_top); pendingintent pendingintent = pendingintent.getactivity(this, 0, intent,pendingintent.flag_one_shot); uri defaultsounduri = ringtonemanager.getdefaulturi(ringtonemanager.type_notification); notificationcompat.builder notificationbuilder = new notificationcompat.builder(this) .setcontenttitle(remotemessage.getfrom()) .setcontenttext(remotemessage.getnotification().getbody()) //.setcontenttext(remotemessage.getdata().tostring()) .setautocancel(true) .setsound(defaultsounduri) .setcontentintent(pendingintent); notificationmanager notificationmanager = (notificationmanager) getsystemservice(context.notification_service); notificationmanager.notify(0, notificationbuilder.build()); } }
here ist manifest:
<application 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> <service android:name=".myfirebasemessagingservice"> <intent-filter> <action android:name="com.google.firebase.messaging_event"/> </intent-filter> </service> <service android:name=".instanceidservice"> <intent-filter> <action android:name="com.google.firebase.instanceid_event"/> </intent-filter> </service> </application>
i hope can me! best regards, felix
seems made small mistake here ..
typo in manifest instance id service intent filter. check below code.
replace
<action android:name="com.google.firebase.instanceid_event"/>
with
<action android:name="com.google.firebase.instance_id_event"/>
and try again.
note :
- try exiting / close app , check firebase console. receive notification default. don't need write logic create notification here.
- if app running , received notification, not appear above can able print in log cat , check.
let me know if helps..
Comments
Post a Comment