【发布时间】:2021-02-03 07:28:41
【问题描述】:
我创建了一个 android 应用程序并希望使用 firebase 进行通知。但我想让电话一直响,即使它处于静音/振动/dnd 模式。
我编写的以下代码在某些情况下不起作用,例如有人确实降低了警报音量/以及在某些手机中无法使用静音模式。
这是我的 FirebaseMessageReceiver.java 文件
public class FirebaseMessageReceiver extends FirebaseMessagingService {
private static final String TAG = "";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
RemoteMessage.Notification notification = remoteMessage.getNotification();
Map<String, String> data = remoteMessage.getData();
Log.d(TAG, "myFirebaseMessagingService - onMessageReceived - message: " + remoteMessage);
Intent dialogIntent = new Intent(this, NotificationReceiver.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
dialogIntent.putExtra("msg", remoteMessage);
startActivity(dialogIntent);
}
private void sendNotification(RemoteMessage.Notification notification, Map<String, String> data) {
String channel_id = getString(default_notification_channel_id);
String channel_name = getString(default_notification_channel_name);
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
Uri notification_sound = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.alert);
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
intent.putExtra("message", notification.getBody());
intent.putExtra("title", notification.getTitle());
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Log.d(TAG, "Alert Sound Value" + notification_sound);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channel_id)
.setContentTitle(notification.getTitle())
.setContentText(notification.getBody())
.setAutoCancel(true)
.setSound(notification_sound)
.setContentIntent(pendingIntent)
.setContentInfo(notification.getTitle())
.setLargeIcon(icon)
.setColor(Color.RED)
.setLights(Color.RED, 1000, 300)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setShowWhen(true)
// .setDefaults(Notification.DEFAULT_VIBRATE)
.setSmallIcon(R.mipmap.ic_launcher);
try {
String picture_url = data.get("picture_url");
if (picture_url != null && !"".equals(picture_url)) {
URL url = new URL(picture_url);
Bitmap bigPicture = BitmapFactory.decodeStream(url.openConnection().getInputStream());
notificationBuilder.setStyle(
new NotificationCompat.BigPictureStyle().bigPicture(bigPicture).setSummaryText(notification.getBody())
);
}
} catch (IOException e) {
e.printStackTrace();
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
setupChannels(notificationManager);
}
notificationManager.notify(1, notificationBuilder.build());
startActivity(intent);
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void setupChannels(NotificationManager notificationManager) {
String adminChannelName = getString(default_notification_channel_name);
String adminChannelDescription = getString(default_notification_channel_name);
String channelid = getString(default_notification_channel_id);
AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
Uri notification_sound = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.i_phone_mix);
NotificationChannel adminChannel;
adminChannel = new NotificationChannel(channelid, adminChannelName, NotificationManager.IMPORTANCE_HIGH);
adminChannel.setDescription(adminChannelDescription);
adminChannel.enableLights(true);
adminChannel.setLightColor(Color.RED);
adminChannel.enableVibration(true);
adminChannel.canBypassDnd();
Log.d(TAG, "Alert Sound Value" + notification_sound);
AudioAttributes audio_attribute = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
audioManager.setStreamVolume(AudioManager.STREAM_ALARM,
audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM),
0);
adminChannel.setSound(notification_sound, audio_attribute);
if (notificationManager != null) {
notificationManager.createNotificationChannel(adminChannel);
}
}
}
【问题讨论】:
标签: android firebase push-notification firebase-cloud-messaging android-notifications