java - 即使应用程序关闭,如何像真正的来电者一样显示警报?
问题描述
我正在开发一个 SOS 应用程序。
当在 Android 应用程序中收到通知时,我想打开一个警报弹出窗口,其中将显示发件人和 SOS 图标的一些详细信息,并且还会响起持续的警报音。
我是通过使用下面的代码来做到这一点的,但是在 android 9 上,Activity 没有启动。当应用程序处于终止状态时,此代码不会启动活动,也不能在后台模式下工作。
private void sendNotification(String messageBody, String notificationType, String notificationId) {
try {
Intent intent = new Intent(this, SosPopUpActivity.class);
intent.putExtra("message", messageBody);
intent.putExtra("notification_type", notificationType);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(
channelId, channelName, importance);
notificationManager.createNotificationChannel(mChannel);
}
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_notification)
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher))
.setContentTitle(getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
.setContentText(messageBody)
.setAutoCancel(false)
.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getApplicationContext().getPackageName() + "/" + R.raw.sos))
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationBuilder.setSmallIcon(R.drawable.ic_notification);
notificationBuilder.setColor(getResources().getColor(R.color.colorPrimary));
//notificationBuilder.setColor(getResources().getColor(R.color.notification_color));
} else {
notificationBuilder.setSmallIcon(R.drawable.ic_notification);
}
notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
// NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// notificationManager.notify(1234, notificationBuilder.build());
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
notificationBuilder.setContentIntent(resultPendingIntent);
notificationManager.notify(1234, notificationBuilder.build());
startActivity(intent);
}catch (Exception e)
{
e.printStackTrace();
AlertDialogHelper.showAlertDialog(this,e.getLocalizedMessage());
}
}
解决方案
推荐阅读
- node.js - 添加项目不增加,而是结合数字
- java - list.get(0) = 2 上的 Java“预期变量”错误
- c# - 如何在C#中使用鼠标在不在图片框中的图像上绘制矩形框
- dynamic - 画面中带有颜色图例的动态工作表标题
- laravel - Laravel Controller 不存在,但实际上存在
- azure - 有没有办法将资源属性或标签中的键值扩展为天蓝色资源图中自己的列?
- android - 在Android中动态加载.dex文件时出现“ClassNotFound”异常
- kubernetes - 如何编写 Kubernetes 自定义控制器来操作云中的 VM 资源?
- python - 在 Pandas 数据框中按 KMeans 集群分组
- java - org.springframework.orm.ObjectOptimisticLockingFailureException 乐观锁定失败...行被另一个事务更新或删除