android - 尝试从 Firebase 向单个设备发送通知时应用程序崩溃

标签 android push-notification firebase firebase-cloud-messaging firebase-notifications

我正在尝试向单个设备发送 Firebase 通知,为此我已使用以下代码检索了 FCM 注册 token :

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    private static final String TAG = "MyFirebaseIIDService";

    /**
     * Called if InstanceID token is updated. This may occur if the security of
     * the previous token had been compromised. Note that this is called when the InstanceID token
     * is initially generated so this is where you would retrieve the token.
     */
    // [START refresh_token]
    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);

        // TODO: Implement this method to send any registration to your app's servers.
        sendRegistrationToServer(refreshedToken);
    }
    // [END refresh_token]

    /**
     * Persist token to third-party servers.
     *
     * Modify this method to associate the user's FCM InstanceID token with any server-side account
     * maintained by your application.
     *
     * @param token The new token.
     */
    private void sendRegistrationToServer(String token) {
        // Add custom implementation, as needed.
    }
}

这是 MyFirebaseMessagingService.java 文件的代码:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";

    /**
     * Called when message is received.
     *
     * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
     */
    // [START receive_message]
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO(developer): Handle FCM messages here.
        // If the application is in the foreground handle both data and notification messages here.
        // 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.
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());

        sendNotification(remoteMessage.getNotification().getBody());
    }
    // [END receive_message]

    /**
     * Create and show a simple notification containing the received FCM message.
     *
     * @param messageBody FCM message body received.
     */
    public void sendNotification(String messageBody) {
        Intent intent = new Intent(this, SettingsActivity.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("FCM Message")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}

问题是,当我尝试在应用仍在屏幕上时发送 Firebase 通知时,它会崩溃并多次出现此错误: java.lang.IllegalArgumentException: URI: content://com. android.contacts/phone_lookup/?encrypt=%20%3C%202,调用用户:com.android.mms:10015,调用包:com.android.mms

我真的不知道这里出了什么问题!

请告诉我。

最佳答案

尝试调试你的onMessageReceived(),如果你没有在推送中发送通知 block ,你必须得到remoteMessage.getNotification() = null:

@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        // Check if remoteMessage.getNotification() == null ??
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); 

        sendNotification(remoteMessage.getNotification().getBody());
    }

同时使用完整的错误日志更新您的问题。

关于android - 尝试从 Firebase 向单个设备发送通知时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38112013/

相关文章:

java - 如何在 ListView 的 3 个项目上应用单击或如何在 Android 中将 3 个项目组合为单击

iOS Swift Twilio 可编程聊天推送通知

java - 如何从 Firebase 数据库中的子项检索特定数据

javascript - 评分系统JavaScript不会将结果发送到Firebase

android - 如何在隐身标签中打开chrome

java - 安卓工作室 : Application crashes at start

android - 如何通过Android中的耳机发送音频输出?

amazon-web-services - AWS Lambda 和 Pusher 集成。如何发送通知?

json - 如何实现推送通知?

firebase - 如何获取集合 Cloud Firestore 中的文档 ID 列表?