Android:Firebase 云消息传递不发送通知以构建 .apk

New*_*per 5 java android firebase firebase-cloud-messaging

当我通过 USB 电缆将手机连接到 Android Studio 时,推送通知正在工作,但是当我生成构建.apk并将其安装在手机上时,应用程序没有收到任何推送通知。

我是 android 新手,第一次尝试推送通知。

输入代码 Manifest.xml

<service
            android:name=".MyFirebaseInstanceIDService">
            android:stopWithTask="false">
            <intent-filter>

                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
Run Code Online (Sandbox Code Playgroud)

类扩展代码FirebaseMessagingService

public class MyFirebaseInstanceIDService extends FirebaseMessagingService {
    @Override
    public void onNewToken(String s) {
        Log.e("NEW_TOKEN", s);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Map<String, String> params = remoteMessage.getData();
        JSONObject object = new JSONObject(params);
        Log.e("JSON_OBJECT", object.toString());

        String NOTIFICATION_CHANNEL_ID = "Nilesh_channel";

        long pattern[] = {0, 1000, 500, 1000};

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

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

            notificationChannel.setDescription("");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setVibrationPattern(pattern);
            notificationChannel.enableVibration(true);

            mNotificationManager.createNotificationChannel(notificationChannel);
        }

        // to diaplay notification in DND Mode
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = mNotificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID);
            channel.canBypassDnd();
        }

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

        notificationBuilder.setAutoCancel(true)
                .setColor(ContextCompat.getColor(this, R.color.colorAccent))
                .setContentTitle(getString(R.string.app_name))
                .setContentText(remoteMessage.getNotification().getBody())
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.ic_action_name)
                .setAutoCancel(true);


        mNotificationManager.notify(1000, notificationBuilder.build());
    }
}
Run Code Online (Sandbox Code Playgroud)

MainActivtiy.java 类与 Firebase 没有任何关联。

任何建议将不胜感激。谢谢。

Rav*_*avi 4

请登录到您的 Firebase 控制台并打开您的项目,然后找到如下项目设置: 在此输入图像描述

然后单击项目设置并进入该页面,您将找到您的应用程序和添加指纹的选项

添加指纹

在此输入图像描述

只需在那里添加您的指纹,然后单击“保存”即可。

添加指纹后,只需从那里下载 google-jsonconfig 文件

下载最新的配置文件

并将该配置文件放入您的项目中并再次构建您的应用程序。

希望这次一切顺利。

而且,如果您需要进一步帮助获取 SHA1 密钥(指纹),请再次发表评论,我会尽力帮助您。

现在,要从 android studio 获取发布和调试指纹,请按照以下步骤操作:

1.转到位于 Android Studio 右侧的 Gradel 部分。

在此输入图像描述

2. 你会发现:应用程序点击它并转到任务->android->signingReport

在此输入图像描述

3.双击signingReport并等待,直到在android studio底部的Run选项卡中生成signingRreport。

4.现在您将拥有如下格式:

Variant: release
Config: release
Store: /home/hp/.android/release.keystore
Alias: AndroidReleaseKey
MD5: Your MD5 key here(xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx)
SHA1: Your fingerprint key here(xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx)
Valid until: Thursday, 20 August, 2048

Variant: debug
Config: debug
Store: /home/hp/.android/debug.keystore
Alias: AndroidDebugKey
MD5: Your MD5 key here(xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx)
SHA1: Your fingerprint key here(xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx)
Valid until: Thursday, 20 August, 2048
Run Code Online (Sandbox Code Playgroud)

从发布版本中,复制您的 SHA1 密钥并将其添加到 firebase。我希望这能解决您现在的问题。