我的应用程序定位到Api Level 27
,当我发送通知时,它成功地第一次接收到通知,无论应用程序是关闭还是打开。
但是之后,它无法显示通知
我想知道在oreo中是否定义了任何时间间隔。
我正在使用最新的Firebase SDK。
实现'com.google.firebase:firebase-messaging:17.3.3'
Firebase实施。
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String from = remoteMessage.getFrom();
Map data = remoteMessage.getData();
Log.d("MSG", "onMessageReceived: "+data);
JSONObject jsonObject = new JSONObject();
Set<String> keys = remoteMessage.getData().keySet();
for (String key : keys) {
try {
jsonObject.put(key, remoteMessage.getData().get(key));
} catch (JSONException e) {
e.printStackTrace();
}
}
try {
SharedPreferences appPrefs = mContext.getSharedPreferences(SHARD_PREF, Context.MODE_PRIVATE);
String notifcation_settings = appPrefs.getString(NOTIFICATION_ENABLED, "");
message= jsonObject.getString("message");
if(!jsonObject.isNull("urlToOpen")){
urlToOpen= jsonObject.getString("urlToOpen");
}
sendNotification(message, urlToOpen);
} catch (JSONException e) {
e.printStackTrace();
}
}
private void sendNotification(final String msg, final String urlToOpen) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent = PendingIntent.getActivity(getApplicationContext(), 0,
notificationIntent, 0);
}
Long getCurrentTime= System.currentTimeMillis();
int id= Math.abs(getCurrentTime.intValue());
mBuilder = new Notification.Builder(mContext);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel();
mBuilder.setChannelId(getResources().getString(R.string.notification_channel_general));
}
mBuilder
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setContentIntent(intent)
.setContentTitle(getResources().getString(R.string.notification_title)).setContentText(msg)
.setStyle(new Notification.BigTextStyle().bigText(msg))
.setSound(soundUri)
.build();
notificationManager.notify("packageIid , mBuilder.build());
}
});
}
private void createNotificationChannel() {
String CHANNEL_ID= getResources().getString(R.string.notification_channel_general);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.enableVibration(true);
channel.setDescription(description);
channel.enableLights(true);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}