Firebase Notification Not Showing on Android Device -
i trying develop module show notifications using firebase. module has 3 function subscribe , unsubscribe topics. has 2 services mentioned in firebase sample
i know working because syncing topics firebase console. new topics being created.
this module connected main app module uses it.
my problem when notification sent, don't receive.
firebase module code :
android manifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ineqe.firebasenotification"> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.call_phone" /> <uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.read_external_storage" /> <uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.receive_boot_completed" /> <uses-permission android:name="android.permission.vibrate" /> <uses-permission android:name="android.permission.wake_lock" /> <uses-permission android:name="android.permission.read_phone_state" /> <!-- plugin --> <uses-permission android:name="android.permission.read_contacts" /> <uses-permission android:name="android.permission.access_fine_location" /> <uses-permission android:name="com.android.vending.check_license" /> <uses-permission android:name="android.permission.get_accounts" /> <uses-permission android:name="com.google.android.c2dm.permission.receive" /> <application android:allowbackup="true" android:label="@string/app_name" android:supportsrtl="true"> <service android:name=".ablefirebasemessagingservice" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.google.firebase.messaging_event" /> </intent-filter> </service> <service android:name=".firebaseidservice"> <intent-filter> <action android:name="com.google.firebase.instance_id_event" /> </intent-filter> </service> </application> </manifest>
ablefirebasemessagingservice
import android.app.notificationmanager; import android.app.pendingintent; import android.content.context; import android.content.intent; import android.media.ringtonemanager; import android.net.uri; import android.support.v7.app.notificationcompat; import android.util.log; import com.google.firebase.messaging.firebasemessagingservice; import com.google.firebase.messaging.remotemessage; import com.ineqe.able.library.mainactivity; public class ablefirebasemessagingservice extends firebasemessagingservice { private static final string tag = "ablefirebaseservice"; @override public void onmessagereceived (remotemessage remotemessage){ // [start_exclude] // there 2 types of messages data messages , notification messages. data messages handled // here in onmessagereceived whether app in foreground or background. data messages type // traditionally used gcm. notification messages received here in onmessagereceived when app // in foreground. when app in background automatically generated notification displayed. // when user taps on notification returned app. messages containing both notification // , data payloads treated notification messages. firebase console sends notification // messages. more see: https://firebase.google.com/docs/cloud-messaging/concept-options // [end_exclude] // todo(developer): handle fcm messages here. // not getting messages here? see why may be: log.d(tag, "from: " + remotemessage.getfrom()); // check if message contains data payload. if (remotemessage.getdata().size() > 0) { log.d(tag, "message data payload: " + remotemessage.getdata()); } // check if message contains notification payload. if (remotemessage.getnotification() != null) { log.d(tag, "message notification body: " + remotemessage.getnotification().getbody()); } // if intend on generating own notifications result of received fcm // message, here should initiated. see sendnotification method below. } // [end receive_message] /** * create , show simple notification containing received fcm message. * * @param messagebody fcm message body received. */ private void sendnotification(string messagebody) { intent intent = new intent(this, mainactivity.class); intent.addflags(intent.flag_activity_clear_top); pendingintent pendingintent = pendingintent.getactivity(this, 0 /* request code */, intent, pendingintent.flag_one_shot); uri defaultsounduri = ringtonemanager.getdefaulturi(ringtonemanager.type_notification); notificationcompat.builder notificationbuilder = (notificationcompat.builder) new notificationcompat.builder(getapplicationcontext()) .setsmallicon(r.drawable.ic_launcher) .setcontenttitle("fcm message") .setcontenttext(messagebody) .setautocancel(true) .setsound(defaultsounduri) .setcontentintent(pendingintent); notificationmanager notificationmanager = (notificationmanager) getsystemservice(context.notification_service); notificationmanager.notify(0 /* id of notification */, notificationbuilder.build()); } }
firebaseidservice
package com.ineqe.firebasenotification; import android.util.log; import com.google.firebase.iid.firebaseinstanceid; import com.google.firebase.iid.firebaseinstanceidservice; /** * created brendan on 26/10/2016. */ public class firebaseidservice extends firebaseinstanceidservice { private static final string tag = "firebaseidservice"; @override public void ontokenrefresh() { // updated instanceid token. string refreshedtoken = firebaseinstanceid.getinstance().gettoken(); log.d(tag, "refreshed token: " + refreshedtoken); // todo: implement method send registration app's servers. sendregistrationtoserver(refreshedtoken); } /** * persist token third-party servers. * <p> * modify method associate user's fcm instanceid token server-side account * maintained application. * * @param token new token. */ private void sendregistrationtoserver(string token) { // add custom implementation, needed. } }
firebasepushservice
import android.util.log; import com.google.firebase.iid.firebaseinstanceid; import com.google.firebase.messaging.firebasemessaging; import com.ineqe.able.library.pushservice; /** * created brendan on 25/10/2016. */ public class firebasepushservice implements pushservice { @override public void initialize() { log.v("token", firebaseinstanceid.getinstance().gettoken()); firebasemessaging.getinstance().subscribetotopic("global"); } @override public void addchannel(string channel) { firebasemessaging.getinstance().subscribetotopic(channel); } @override public void removechannel(string channel) { firebasemessaging.getinstance().unsubscribefromtopic(channel); } }
also add manifest, gradle.build file , main activity file show how reference firebase module.
manifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ineqe.hsct.fostering"> <uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.read_external_storage" /> <permission android:name="com.ineqe.hsct.fostering.permission.c2d_message" android:protectionlevel="signature" /> <uses-permission android:name="com.ineqe.hsct.fostering.permission.c2d_message" /> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsrtl="true" android:name=".myapplication" android:theme="@style/apptheme"> <meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/ic_not" /> <activity android:name=".walkthroughactivity" android:theme="@style/apptheme.noactionbar" /> <activity android:name=".stafdirectorydetailactivity" android:theme="@style/apptheme2" /> <activity android:name=".mainactivity" android:theme="@style/theme.myapp" android:configchanges="screensize|orientation" android:label="@string/app_name"> </activity> <activity android:name=".expansionactivity" android:configchanges="screensize|orientation"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
myapplication.java
import com.ineqe.firebasenotification.firebasepushservice; public class myapplication extends com.ineqe.able.library.myapplication { @override public void oncreate() { setmainactivity(mainactivity.class); settintappcolor("#00b5cc"); setsplashimage("hsctf_splash_bg"); setapptracker("ua-42956664-12"); firebasepushservice ps = new firebasepushservice(); ps.initialize(); setpushservice(ps); setloginimage("hsctf_login_bg"); setschool(true); settesting(false); setanswerspage(true); setexpansionfiles(false); super.oncreate(); } }
gradle.build
apply plugin: 'com.android.application' android { compilesdkversion 23 buildtoolsversion "23.0.2" uselibrary 'org.apache.http.legacy' defaultconfig { applicationid "com.ineqe.hsct.fostering" minsdkversion 16 targetsdkversion 23 multidexenabled true versioncode 19 versionname "1.0" multidexenabled true } buildtypes { release { minifyenabled false proguardfiles getdefaultproguardfile('proguard-android.txt'), 'proguard-rules.pro' } } packagingoptions { exclude 'meta-inf/license.txt' } } repositories { maven { url 'https://maven.fabric.io/public' } } dependencies { compile filetree(dir: 'libs', include: ['*.jar']) testcompile 'junit:junit:4.12' compile 'me.relex:circleindicator:1.2.1@aar' compile 'com.android.support:appcompat-v7:23.2.0' compile project(':able') compile project(":firebasenotification") }
what know right :
- the app can talk firebase console , create new topics.
- notification doesn't seem arrive when use token specific device working with.
if have ideas don't hesitate comment
first add filter in firebasemessanging service:
<action android:name="android.intent.action.respond_via_message"/>
in firebasemessanging:
@override public void onmessagereceived(remotemessage remotemessage) { super.onmessagereceived(remotemessage); remotemessage.notification notification = remotemessage.getnotification(); map<string, string> data = remotemessage.getdata(); shownotification(notification, data); } private void shownotification(remotemessage.notification notification, map<string, string> data) { bitmap icon = bitmapfactory.decoderesource(getresources(), r.mipmap.ic_launcher); 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 sound = uri.parse("android.resource://" + getapplicationcontext().getpackagename() + "/raw/notification"); android.support.v4.app.notificationcompat.builder notificationbuilder = new notificationcompat.builder(this) .setcontenttitle(data.get("title")) .setcontenttext(data.get("text")) .setautocancel(true) .setsound(sound) .setcontentintent(pendingintent) .setcontentinfo("any") .setlargeicon(icon) .setcolor(color.red) .setsmallicon(r.mipmap.ic_launcher); notificationbuilder.setdefaults(notification.default_vibrate); notificationbuilder.setlights(color.yellow, 1000, 300); notificationmanager notificationmanager = (notificationmanager) getsystemservice(context.notification_service); notificationmanager.notify(0, notificationbuilder.build()); }
and in firebaseidservice:
@override public void ontokenrefresh() { super.ontokenrefresh(); string refreshedtoken = firebaseinstanceid.getinstance().gettoken(); firebasemessaging.getinstance().subscribetotopic("globalgroup"); // sendregistrationtoserver(refreshedtoken); method save tokens }
Comments
Post a Comment