FCM Token Issue in some android device

一曲冷凌霜 提交于 2019-12-05 02:06:50

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());}}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!