【问题标题】:Android: Firebase Cloud Messaging not sending notification to build .apkAndroid:Firebase Cloud Messaging 未发送通知以构建 .apk
【发布时间】:2018-12-18 07:52:36
【问题描述】:

当我通过 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>

类扩展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());
    }
}

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

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

【问题讨论】:

  • 您为后端开发人员提供了哪个 API 密钥?

标签: java android firebase firebase-cloud-messaging


【解决方案1】:

请登录您的 Firebase 控制台并打开您的项目,然后找到如下项目设置:

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

添加指纹

只需在此处添加您的指纹并点击保存即可。

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

下载最新的配置文件

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

希望这次一切顺利。

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

现在,从 android studio 获取您的发布和调试指纹 按照这个:

1.转到位于您的 android 工作室右侧的 Gradel 部分。

2。你会在那里找到:应用点击它并转到 Task->android->signingReport

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

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

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

【讨论】:

  • 谢谢,我按照上述步骤从 Android Studio 添加了调试 SHA1 并将 google-jsonconfig 文件放置到 android studio > Generated Signed apk 但它仍然无法正常工作。如果我将设备连接到计算机,则工作正常。知道我可能做错了什么吗?
  • 当您的设备连接到计算机时,意味着安装在其上的 apk 是调试 apk,并且您提供的调试 SHA1 指纹是正确的,这就是它工作的原因,但为了在发布版本中获得推送通知您还需要将发布指纹添加到 Firebase。我正在编辑我的上述评论并添加如何获取您的发布指纹。
【解决方案2】:

确保在控制台上添加两个指纹 - 来自 debugrelease 键。

正如你所描述的,这听起来很相似,只是在那里添加了 debug 键......

添加后,google-services.json 需要下载和更新以反映这一点。

【讨论】:

  • 对不起,我不知道它们是什么。您能否提供我可以生成/添加它们的任何资源?那真是太好了。
  • 谢谢马丁,我从 android studio 获得了调试 SHA1 密钥,并使用 KeyStore Explorer 工具从签名中提取 SHA1 以获得 SHA1 以供发布我相信。将这两个粘贴到 Firebase 并使用新的 google json 文件更新 android studio > 生成另一个签名的 pk 但仍然无法正常工作。
  • @NewbieDeveloper 这些通常是两个完全不同的密钥库,具有两个完全不同的密钥和两个完全不同的指纹。除非同时添加它们,否则这是行不通的。您可能必须创建一个发布密钥库和密钥,然后使用它来签署apk - 并将其添加到 Firebase 控制台。只需查看文档:developer.android.com/studio/publish/app-signing
【解决方案3】:

尝试按照以下步骤操作

->为调试和发布密钥库文件生成sha1

keytool -list -v -keystore {keystore_name} -alias {alias_name}

例如:

keytool -list -v -keystore C:\Users\me\Desktop\release.jks -alias sample

->将这些 sha1 添加到 Firebase 控制台

【讨论】:

    【解决方案4】:

    我通过从 Firebase 控制台更改我的服务器密钥来解决这个问题 -> 项目设置 -> 云消息传递 -> 服务器密钥

    这是我需要更改的一段代码

    public interface APIService {
        @Headers(
    
                {
                        "Content-Type:application/json", "Authorization:key=(Find this key from Cloud messaging tab in firebase)"
    
                }
        )
    
        @POST("fcm/send")
        Call<MyResponse> sendnotification(@Body Sender body);
    }
    

    谢谢,希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-05
      • 1970-01-01
      • 2020-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多