Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FCM Token Issue in some android device

FCM Expert Please Help me out,I have to integrate notification for the android using FCM, I am trying the demo given by Firebase, I tried many logic but it's giving first token for the some devices: I used three device, Moto E (2nd Gen)(android version 5.1.1), Samsung GT-S7562(A.V 4.1.2) and Karbon Titanium (A.V 4.2.2), I am receiving the token only for Karbon Titanium ,don't know why I am not receiving the token for Moto E(2 Gen)and Samsung GT-S7562 but receiving the token from karbon Titanium device :

Here is the code I tried :

Logcat Moto E2 :

07-07 15:58:08.776 24152-24152/? I/art: Late-enabling -Xcheck:jni
07-07 15:58:08.802 24152-24162/? E/art: Failed sending reply to debugger: Broken pipe
07-07 15:58:08.802 24152-24162/? I/art: Debugger is no longer active
07-07 15:58:08.848 24152-24152/? D/FirebaseApp: com.google.firebase.auth.FirebaseAuth is not linked. Skipping initialization.
07-07 15:58:08.859 24152-24152/? D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
07-07 15:58:08.872 24152-24152/? I/FA: App measurement is starting up, version: 9256
07-07 15:58:08.872 24152-24152/? I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
07-07 15:58:09.027 24152-24152/? I/FirebaseInitProvider: FirebaseApp initialization successful
07-07 15:58:09.196 24152-24152/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
07-07 15:58:09.361 24152-24192/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
07-07 15:58:09.378 24152-24152/? D/Atlas: Validating map...
07-07 15:58:09.564 24152-24192/? I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build:  (Ibddc658e36)
                                               OpenGL ES Shader Compiler Version: E031.25.03.04
                                               Build Date: 08/10/15 Mon
                                               Local Branch: workspace
                                               Remote Branch: 
                                               Local Patches: 
                                               Reconstruct Branch: 
07-07 15:58:09.567 24152-24192/? I/OpenGLRenderer: Initialized EGL, version 1.4
07-07 15:58:09.609 24152-24192/? D/OpenGLRenderer: Enabling debug mode 0
07-07 15:58:12.860 24152-24242/com.shubhank.fcm_tutorial W/InstanceID/Rpc: Found 10016
07-07 15:58:13.049 24152-24242/com.shubhank.fcm_tutorial D/FirebaseInstanceId: background sync failed: PHONE_REGISTRATION_ERROR, retry in 10s
07-07 15:58:19.256 24152-24178/com.shubhank.fcm_tutorial I/FA: Tag Manager is not found and thus will not be used
07-07 15:58:23.194 24152-24448/com.shubhank.fcm_tutorial D/FirebaseInstanceId: background sync failed: PHONE_REGISTRATION_ERROR, retry in 20s

Logcat of Karbon Mobile:

07-07 16:38:03.964 3939-3939/com.shubhank.fcm_tutorial I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
07-07 16:38:04.004 3939-3939/com.shubhank.fcm_tutorial I/FirebaseInitProvider: FirebaseApp initialization successful
07-07 16:38:04.017 3939-3939/com.shubhank.fcm_tutorial W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
07-07 16:38:04.017 3939-3939/com.shubhank.fcm_tutorial I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
07-07 16:38:04.017 3939-3939/com.shubhank.fcm_tutorial W/dalvikvm: VFY: unable to resolve interface method 19193: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
07-07 16:38:04.017 3939-3939/com.shubhank.fcm_tutorial I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
07-07 16:38:04.017 3939-3939/com.shubhank.fcm_tutorial W/dalvikvm: VFY: unable to resolve interface method 19197: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
07-07 16:38:04.049 3939-3939/com.shubhank.fcm_tutorial I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
07-07 16:38:04.049 3939-3939/com.shubhank.fcm_tutorial W/dalvikvm: VFY: unable to resolve virtual method 577: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
07-07 16:38:04.050 3939-3939/com.shubhank.fcm_tutorial I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
07-07 16:38:04.050 3939-3939/com.shubhank.fcm_tutorial W/dalvikvm: VFY: unable to resolve virtual method 599: Landroid/content/res/TypedArray;.getType (I)I
07-07 16:38:04.092 3939-3939/com.shubhank.fcm_tutorial I/dalvikvm: Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawable
07-07 16:38:04.092 3939-3939/com.shubhank.fcm_tutorial W/dalvikvm: VFY: unable to resolve virtual method 540: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
07-07 16:38:04.092 3939-3939/com.shubhank.fcm_tutorial I/dalvikvm: Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawableForDensity
07-07 16:38:04.092 3939-3939/com.shubhank.fcm_tutorial W/dalvikvm: VFY: unable to resolve virtual method 542: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
07-07 16:38:04.096 3939-3939/com.shubhank.fcm_tutorial W/notification: faQNkJ65F5c:APA91bFHH5rVOhIlX5k7Pxo-sbZPwZplWqBGLVOjlG8G4NwoMqaKZQRYWMMjliYXb0gokiuJPBYpWY0G7L-FEbgAv7LfKcuR0j7Sr6ATxb1j2UR7A
07-07 16:38:04.148 3939-3939/com.shubhank.fcm_tutorial I/MaliEGL: [Mali]window_type=1, is_framebuffer=0, errnum = 0
07-07 16:38:04.148 3939-3939/com.shubhank.fcm_tutorial I/MaliEGL: [Mali]surface->num_buffers=4, surface->num_frames=3, win_min_undequeued=1
07-07 16:38:04.148 3939-3939/com.shubhank.fcm_tutorial I/MaliEGL: [Mali]max_allowed_dequeued_buffers=3
07-07 16:38:04.159 3939-3939/com.shubhank.fcm_tutorial I/[MALI][Gralloc]: dlopen libsec_mem.so fail

MainActivity:

    public class MainActivity extends AppCompatActivity {
    Tracker mTracker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String token = FirebaseInstanceId.getInstance().getToken();
        if (token != null) {
            Log.w("notification", token);
            Toast.makeText(MainActivity.this,""+token,Toast.LENGTH_SHORT).show();
        }

    }


}

app gradle :

apply plugin: 'com.android.application'
   android {
        compileSdkVersion 23
        buildToolsVersion "23.0.3"

        defaultConfig {
            applicationId "com.shubhank.fcm_tutorial"
            minSdkVersion 15
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled true
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile 'com.google.android.gms:play-services-analytics:9.2.0'

        compile 'com.android.support:appcompat-v7:23.3.0'
        compile 'com.google.firebase:firebase-core:9.0.1'
        compile 'com.google.firebase:firebase-messaging:9.0.1'
        compile 'com.android.support:design:23.3.0'
    }


    apply plugin: 'com.google.gms.google-services'

top level gradle :

buildscript {
    repositories {
        jcenter()
        mavenLocal()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        classpath 'com.google.gms:google-services:3.0.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        mavenLocal()
    }
}

TokenService:

public class TokenService extends FirebaseInstanceIdService {

@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.w("notification", refreshedToken);
    sendRegistrationToServer(refreshedToken);
}

private void sendRegistrationToServer(String token) {
}
}

FCMMessageReceiverService:

public class FCMMessageReceiverService extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.w("fcm", "received notification");
    sendNotification(remoteMessage.getNotification().getTitle());
}

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 = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(messageBody)
            .setAutoCancel(false)
            .setSound(defaultSoundUri);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(1, notificationBuilder.build());
}
}

Manifest File:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.shubhank.fcm_tutorial">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:launchMode="standard"
        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=".TokenService"
            android:exported="true">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>
        <service android:name=".FCMMessageReceiverService"
            android:exported="true">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
        </activity>
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"/>
    </application>

</manifest>
like image 630
Barnali Bhattacharjee Avatar asked Jul 06 '16 08:07

Barnali Bhattacharjee


1 Answers

Make changes like this

in gradle file

   compile 'com.google.firebase:firebase-messaging:9.0.0'

and

public class FirebaseMessagingServices extends FirebaseMessagingService {

private static final String TAG = "FirebaseMessaging";
private NotificationManager mNotificationManager;
public static final int NOTIFICATION_ID = 1;


@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // TODO(developer): Handle FCM messages here.
    // Not getting messages here
    Log.v(TAG, "From: " + remoteMessage.getFrom());

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.v(TAG, "Message data payload: 1" + remoteMessage.getData());
      //  sendNotification(remoteMessage.getData()+"");
        Log.v(TAG, "Message data payload: 2" + remoteMessage.getData().get(Config.MESSAGE_KEY).toString());    
                    sendNotification(remoteMessage.getData().get(Config.MESSAGE_KEY) + "");
         }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.v(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());

        SharedPreferences preferences =getSharedPreferences(Utils.SIGNUP_PREF, MODE_PRIVATE);
        boolean notification = preferences.getBoolean(Utils.notification,true);

            sendNotification(remoteMessage.getNotification().getBody()); 
    }
}
    private void sendNotification(String msg) {
    MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.notification1);
    mp.start();

    mNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, MainActivity.class), 0);

    NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.penless_small)
            .setContentTitle("Penless")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg);
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());}}
like image 83
h_patel Avatar answered Oct 14 '22 05:10

h_patel