【发布时间】: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