[英]Android: Firebase Cloud Messaging not sending notification to build .apk
当我通过 USB 电缆将手机连接到 Android Studio 时,推送通知正在工作,但是当我生成 build .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 相关的链接。
任何建议将不胜感激。 谢谢。
请登录您的 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。 我希望这现在可以解决您的问题。
确保在控制台上添加了两个指纹 - 来自debug
和release
密钥。
正如你所描述的,这听起来很相似,只是在那里添加了debug
键......
添加后,需要下载和更新google-services.json
,以反映这一点。
尝试按照以下步骤操作
-> 为调试和发布密钥库文件生成 sha1
keytool -list -v -keystore {keystore_name} -alias {alias_name}
例如:
keytool -list -v -keystore C:\Users\me\Desktop\release.jks -alias sample
-> 将那些 sha1 添加到 Firebase 控制台
我通过从 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);
}
谢谢,希望这对你有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.