迁移到androidx后,推送通知停止工作

时间:2019-12-25 10:23:31

标签: android push-notification androidx

我已将项目迁移到androidx。但是我没有收到任何通知。在升级到androidx之前,它工作正常。升级后它停止工作。我已实现如下:

public class MyFirebaseInstanceIDService extends FirebaseMessagingService {
@Override
public void onNewToken(@NonNull String s) {
    super.onNewToken(s);

    String refreshedToken = FirebaseInstanceId.getInstance().getInstanceId().getResult().getToken();
    SharedHelper.putKey(getApplicationContext(),"device_token",""+refreshedToken);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
}}

我已经实现了FirebaseMessaging类,如下所示:

public class MyFirebaseMessagingService extends FirebaseMessagingService {


private static final String TAG = "MyFirebaseMsgService";
Utilities utils = new Utilities();

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    if (remoteMessage.getData() != null) {
        sendNotification(remoteMessage.getData().get("message"));
    }else{
        utils.print(TAG,"FCM Notification failed");
    }
}


private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, MainNewActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("Notification",messageBody);
    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)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    notificationBuilder.setSmallIcon(getNotificationIcon(notificationBuilder), 1);

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

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

我的清单:

 <service android:name=".FCM.MyFirebaseMessagingService"
        android:stopWithTask="false"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

    <service android:name=".FCM.MyFirebaseInstanceIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>

我的gradle依赖项:

implementation 'com.google.firebase:firebase-messaging:20.0.0'
implementation 'com.google.firebase:firebase-core:17.1.0'
implementation 'com.google.firebase:firebase-auth:19.0.0'
implementation 'com.google.firebase:firebase-config:19.0.0'
implementation 'com.google.android.gms:play-services-auth:17.0.0'
implementation 'com.google.android.gms:play-services-gcm:17.0.0'

1 个答案:

答案 0 :(得分:0)

我关注docs Google firebase,而不关注MyFirebaseInstanceIDService,可以尝试

class MyFirebaseMessagingService : FirebaseMessagingService() {

    override fun onMessageReceived(p0: RemoteMessage) {
        super.onMessageReceived(p0)
        Log.e("onMessageReceived", "${p0.data}")
    }

    override fun onNewToken(p0: String) {
        super.onNewToken(p0)
        Log.e("onNewToken", p0)

    }

}

在AndroidManifest.xml

<service
            android:name=".services.notificaion.MyFirebaseMessagingService"
            android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>

要获取令牌:

FirebaseInstanceId.getInstance().instanceId
    .addOnCompleteListener(OnCompleteListener { task ->
        if (!task.isSuccessful) {
            Log.w(TAG, "getInstanceId failed", task.exception)
            return@OnCompleteListener
        }

        // Get new Instance ID token
        val token = task.result?.token

        // Log and toast
        val msg = getString(R.string.msg_token_fmt, token)
        Log.d(TAG, msg)
        Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
    })