简体   繁体   English

我无法在我的 android 仿真器中接收 Firebase 云消息传递 FCM

[英]I Can' t receive the Firebase Cloud Messaging FCM In my android emulator

I am working on an application that must receive a push notification and I try everything to make it work but it not working and this is my Code我正在开发一个必须接收推送通知的应用程序,我尽一切努力使其工作,但它不起作用,这是我的代码

public class MyFirebaseInstanceService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        if (remoteMessage.getData().isEmpty()) {

            showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
        }

        else
        {
            showNotification(remoteMessage.getData());

        }

    }

    private void showNotification(Map<String, String> data) {
        String title = data.get("title").toString();
        String body = data.get("body").toString();


        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID = "example.myapplication.service.test";

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,"Notification",
                    NotificationManager.IMPORTANCE_DEFAULT);

            notificationChannel.setDescription("Team Tarang");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.BLUE);
            notificationChannel.enableLights(true);
            notificationManager.createNotificationChannel(notificationChannel);
        }

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);

        notificationBuilder.setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setContentTitle(title)
                .setContentText(body)
                .setContentInfo("Info");

        notificationManager.notify(new Random().nextInt(),notificationBuilder.build());


    }

    private void showNotification(String title, String body) {
        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID = "example.myapplication.service.test";

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,"Notification",
                    NotificationManager.IMPORTANCE_DEFAULT);

            notificationChannel.setDescription("Team Tarang");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.BLUE);
            notificationChannel.enableLights(true);
            notificationManager.createNotificationChannel(notificationChannel);
        }

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);

        notificationBuilder.setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setContentTitle(title)
                .setContentText(body)
                .setContentInfo("Info");

        notificationManager.notify(new Random().nextInt(),notificationBuilder.build());
    }

    @Override
    public void onNewToken(String s) {
        super.onNewToken(s);

        Log.d("TOKENFIREBASE",s);
    }
}

and this is my AndroidManifest.xml Code这是我的 AndroidManifest.xml 代码

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

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        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=".services.MyFirebaseMessagingService"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>

    </application>

</manifest>

and I'm already making the connection by FirebaseAndroid studio tool Like this photo我已经通过 FirebaseAndroid Studio 工具建立了连接 就像这张照片

在此处输入图像描述

please anyone can help me with this issue.请任何人都可以帮助我解决这个问题。

To receive FCM you need Google Play Services installed in your emulator要接收 FCM,您需要在模拟器中安装 Google Play Services

If you are using Android Studio select an emulator with Play Store如果您使用的是 Android Studio select 模拟器和 Play Store

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我没有在我的 Android 应用中收到 Firebase 云消息 - I don't receive the Firebase Cloud Message in my android app addCompleteListener()尚未解决-Firebase Cloud Messaging(FCM) - addCompleteListener() is not being resolved - Firebase Cloud Messaging (FCM) MismatchSenderId错误Firebase云消息传递(FCM) - MismatchSenderId error Firebase Cloud Messaging (FCM) Firebase Cloud Messaging(FCM)无法获取数据 - Firebase Cloud Messaging (FCM) not getting data 如何在 Android 的 firebase 数据库中存储 FireBase 云消息传递令牌? - How can I store FireBase Cloud Messaging tokens in firebase database in Android? 是否可以在没有 Firebase Cloud Messaging (FCM) 的情况下向所有安装了我的应用程序的设备发送推送通知? - is it possible to make push notifications to all devices which had installed my application without Firebase Cloud Messaging (FCM)? Android无法扩展Firebase Messaging Service - Android can't extend Firebase Messaging Service 如何向FCM(Firebase云消息传递)令牌的特定用户发送消息? - How to send a message to a specific user of an FCM (Firebase Cloud Messaging) token? 在Firebase Cloud Messaging上检查新通知。 (FCM) - Check for new notifications on Firebase Cloud Messaging. (FCM) 无法通过 firebase-cloud-messaging 中的 pendingIntent 传递数据 - Can't pass data through pendingIntent in firebase-cloud-messaging
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM