i've got firebase building without warnings using eclipse (see related: unable find obfuscated firebase class in eclipse)
i try send test notification, i'm not getting anything. i'm using library project well. code:
androidmanifest.xml
<application /> ... <!-- ================================== firebase =================================== --> <service android:name=".myfirebasemessagingservice" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.messaging_event" /> </intent-filter> </service> <service android:name=".myfirebaseinstanceidservice" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.instance_id_event" /> </intent-filter> </service> .... </application>
these files practically straight samples:
public class myfirebasemessagingservice extends firebasemessagingservice { public static final string push_notification_text = "pushnotificationtext"; private static final string tag = "firebase"; public static final int notification_id = 1; @override public void onmessagereceived(remotemessage remotemessage) { // handle data payload of fcm messages. string messageid = remotemessage.getmessageid(); remotemessage.notification notification = remotemessage.getnotification(); map<string, string> data = remotemessage.getdata(); log.d(tag, "fcm message id: " + messageid); log.d(tag, "fcm notification message: " + notification); log.d(tag, "fcm data message: " + data); sendnotification(this, notification.tostring()); } // put gcm message notification , post it. private void sendnotification(context context, string message) { notificationmanager mnotificationmanager = (notificationmanager)context.getsystemservice(context.notification_service); final intent intent = new intent(context, splash.class); //intent.putextras(bundle); intent.setflags(intent.flag_activity_single_top); //save push notification message show when app starts sharedpreferences sp = preferencemanager.getdefaultsharedpreferences(context); sp.edit().putstring(push_notification_text, message).commit(); pendingintent contentintent = pendingintent.getactivity(context, 0, intent, pendingintent.flag_one_shot); final notificationcompat.builder mbuilder = new notificationcompat.builder(context) .setautocancel(true) .setvibrate(new long[] { 0, 500, 200, 500, 200, 500 }) .setcontentintent(contentintent) .setcontenttitle(context.getstring(r.string.app_name)) .setsmallicon(r.drawable.ic_launcher) .setstyle(new notificationcompat.bigtextstyle().bigtext(message)) .setcontenttext(message) .setwhen(system.currenttimemillis()).setongoing(false); mnotificationmanager.notify(notification_id, mbuilder.build()); } }
other class:
public class myfirebaseinstanceidservice extends firebaseinstanceidservice { private static final string tag = "firebase"; private static final string friendly_engage_topic = "friendly_engage"; /** * application's current instance id token no longer valid * , new 1 must requested. */ @override public void ontokenrefresh() { // if need handle generation of token, or // after refresh should that. string token = firebaseinstanceid.getinstance().gettoken(); log.d(tag, "fcm token: " + token); // once token generated, subscribe topic. firebasemessaging.getinstance().subscribetotopic(friendly_engage_topic); } }
i send message via firebase console targeting package name no luck. i'm uncertain should work. need use google-services.json
file in way?
thanks
i don't use eclipse , not able verify these steps work. hope started on right path.
the firebase framework initialized firebaseinitprovider. don't need implement or subclass it. declare in manifest.
<provider android:authorities="${applicationid}.firebaseinitprovider" android:name="com.google.firebase.provider.firebaseinitprovider" android:exported="false" android:initorder="100" />
firebaseinitprovider
expects find string resources contain configuration values project. in app built android studio , google services gradle plugin, resource values created google-services.json
file. because not using , plugin, need define these resources in res
folder. values can found in project settings @ firebase console or in google-service.json file
. because interested in messaging, values may not needed. safe, define them initially.
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="default_web_client_id" translatable="false">8888888888888-ooqodhln4cjj4qst7b4sadfiousdf7.apps.googleusercontent.com</string> <string name="firebase_database_url" translatable="false">https://project-888888888888888.firebaseio.com</string> <string name="gcm_defaultsenderid" translatable="false">888888888888</string> <string name="google_api_key" translatable="false">aizasyb0bhr1sfsydfsdfnwelhkoyifak_go2xu</string> <string name="google_app_id" translatable="false">1:888888888888:android:526f9740dfg987sdfg</string> <string name="google_crash_reporting_api_key" translatable="false">aizasydkg-g8hh7t4tv7rrsdfgiopudfmn234897</string> <string name="google_storage_bucket" translatable="false">project-8888888888888888888888.appspot.com</string> </resources>
you can confirm initialization succeeded putting code in oncreate()
method of main activity:
firebaseoptions opts = firebaseapp.getinstance().getoptions(); log.i(tag, "onstart: id=" + opts.getapplicationid()); log.i(tag, "onstart: senderid=" + opts.getgcmsenderid()); log.i(tag, "onstart: key=" + opts.getapikey());
if valid results logged, indicates default firebaseapp has been initialized, , should able receive messages.
Comments
Post a Comment