安卓-Firebase通知无法正常工作

3

我遇到了Firebase通知的问题。我按照文档设置了它,但它没有工作。下面是我的代码。虽然我认为一切都是正确的。

这是我的NotificationsService

public class NotificationsService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        String TAG = "Notification";

        Log.d(TAG, "From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        }

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

        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
    }
}

然后是AndroidManifest.xml文件。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.usefashion.useapp">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.NoActionBar">
        <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=".NotificationsService"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

        <service android:name=".InstanceIdService"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>
    </application>
</manifest>

希望有人能帮助我。我也有一个InstanceIdService,但它不是这个问题的一部分。
编辑1: 我意识到当我通过控制台发送通知时,这条消息会在调试中显示。
E/FirebaseInstanceId: Error resolving target intent service, skipping classname enforcement. Resolved service was: com.taplane.triviaquiz/com.msi.logocore.helpers.thirdparty.firebase.FirebaseServiceListener

这是什么?


当您发送通知时,应用程序是在前台还是后台? - Larry McKenzie
在前台,我要记住我正在调试应用程序,并查看日志以确定是否收到通知。 - Lucas Müller
如果应用程序在后台运行,您是否能看到Firebase为您创建的通知? - Larry McKenzie
你是否正在使用Firebase控制台发送消息?如果是,你确定你的设备在你要发送的组中吗? - Larry McKenzie
是的,我正在发送给具有应用程序“com.usefashion.useapp”的用户段。 - Lucas Müller
显示剩余5条评论
1个回答

0
这是我的 FCM 服务:
=> public class MyFirebaseInstanceIdServices extends FirebaseInstanceIdService { private static final String TAG = "MyFirebaseIIDService";
@Override
public void onTokenRefresh() {
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "FireBase token: " + refreshedToken);
    Prefs.setFirebaseToken(getApplicationContext(), refreshedToken);
}

}

=> 公共类 MyFirebaseMessagingServices 扩展 FirebaseMessagingService { private static final String TAG = "MyFirebaseMsgService";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.d(TAG, "From: " + remoteMessage.getData() + " Message Body");
    Map<String, String> notiMessage = remoteMessage.getData();
    String type = notiMessage.get("type");
    String title = notiMessage.get("title");
    String message = notiMessage.get("message");
    String orderId = notiMessage.get("order_id");
    sendNotification(type, title, message, orderId);
}

//This method is only generating push notification
private void sendNotification(String type, String title, String message, String orderId) {

    if (Prefs.getIsLogin(this)) {
        Intent intent = null;
        if (type.equals("order")) {
            intent = new Intent(this, OrderConfirmationActivity.class);
            intent.putExtra(Constant.ORDER_ID, orderId);
            intent.putExtra("FromNoti", true);
        } else {
            intent = new Intent(this, HomeActivity.class);
        }


        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_marker);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(bitmap)
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

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

}

=> 在清单文件中:

    <service android:name="com.labagel.fcm.MyFirebaseMessagingServices">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

    <service android:name="com.labagel.fcm.MyFirebaseInstanceIdServices">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service> 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接