android - Can't see token in logcat for Firebase Push Notification App -
i trying create app push notifications using firebase (on windows 10) i'm unable token in logcat. code have display token in logcat named refreshed token it's not displaying it, have uninstall app , run app many times still problem same. don't know i'm doing wrong if know solution please post in comment. have attached files
mainactivity.java
package com.example.mnaum.firebasepushnotification; import android.support.v7.app.appcompatactivity; import android.os.bundle; import com.google.firebase.iid.firebaseinstanceid; public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } }
myfirebaseinstanceidservice.java
package com.example.mnaum.firebasepushnotification; import android.util.log; import android.widget.toast; import com.google.firebase.iid.firebaseinstanceid; import com.google.firebase.iid.firebaseinstanceidservice; public class myfirebaseinstanceidservice extends firebaseinstanceidservice { private static final string tag = "myfirebaseiidservice"; @override public void ontokenrefresh() { //getting registration token string refreshedtoken = firebaseinstanceid.getinstance().gettoken(); //displaying token on logcat log.d(tag, "refreshed token: " + refreshedtoken); sendregistrationtoserver(refreshedtoken); } private void sendregistrationtoserver(string token) { //you can implement method store token on server //not required current project } }
myfirebasemessagingservice.java
package com.example.mnaum.firebasepushnotification; 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.v4.app.notificationcompat; import android.util.log; import com.google.firebase.messaging.firebasemessagingservice; import com.google.firebase.messaging.remotemessage; public class myfirebasemessagingservice extends firebasemessagingservice { private static final string tag = "myfirebasemsgservice"; @override public void onmessagereceived(remotemessage remotemessage) { //displaying data in log //it optional log.d(tag, "from: " + remotemessage.getfrom()); log.d(tag, "notification message body: " + remotemessage.getnotification().getbody()); //calling method generate notification sendnotification(remotemessage.getnotification().getbody()); } //this method generating push notification //it same did in earlier posts 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, intent, pendingintent.flag_one_shot); uri defaultsounduri= ringtonemanager.getdefaulturi(ringtonemanager.type_notification); notificationcompat.builder notificationbuilder = new notificationcompat.builder(this) .setsmallicon(r.mipmap.ic_launcher) .setcontenttitle("firebase push notification") .setcontenttext(messagebody) .setautocancel(true) .setsound(defaultsounduri) .setcontentintent(pendingintent); notificationmanager notificationmanager = (notificationmanager) getsystemservice(context.notification_service); notificationmanager.notify(0, notificationbuilder.build()); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.mnaum.firebasepushnotification.mainactivity"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello world!" app:layout_constraintbottom_tobottomof="parent" app:layout_constraintleft_toleftof="parent" app:layout_constraintright_torightof="parent" app:layout_constrainttop_totopof="parent" /> </relativelayout>
androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mnaum.firebasepushnotification"> <uses-permission android:name="android.permission.internet"/> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundicon="@mipmap/ic_launcher_round" 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> <!-- defining services --> <service android:name=".myfirebasemessagingservice"> <intent-filter> <action android:name="com.google.firebase.messaging_event"/> </intent-filter> </service> <service android:name=".myfirebaseinstanceidservice"> <intent-filter> <action android:name="com.google.firebase.instance_id_event"/> </intent-filter> </service> </application> </manifest>
build.gradle (project level)
// top-level build file can add configuration options common sub-projects/modules. buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.3.3' classpath 'com.google.gms:google-services:3.0.0' // note: not place application dependencies here; belong // in individual module build.gradle files } } allprojects { repositories { jcenter() } } task clean(type: delete) { delete rootproject.builddir }
build.gradle (app level)
apply plugin: 'com.android.application' android { compilesdkversion 26 buildtoolsversion "26.0.0" defaultconfig { applicationid "com.example.mnaum.firebasepushnotification" minsdkversion 15 targetsdkversion 26 versioncode 1 versionname "1.0" testinstrumentationrunner "android.support.test.runner.androidjunitrunner" } buildtypes { release { minifyenabled false proguardfiles getdefaultproguardfile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile filetree(dir: 'libs', include: ['*.jar']) androidtestcompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' testcompile 'junit:junit:4.12' compile 'com.google.firebase:firebase-messaging:9.0.0' } apply plugin: 'com.google.gms.google-services'
if using wifi network may google api won't work. if ask admin person go.
may not getting log try copy , past tag in log filter or ide check log.
if getting save sharedprefernce or upload server.
Comments
Post a Comment