android - 如何显示弹出通知,例如什么是应用呼叫通知?
问题描述
我想创建一个弹出式呼叫通知,例如什么是应用程序呼叫通知,但我的通知没有像静态那样显示在顶部,当我向下滑动通知栏时,我会看到通知。
这是我没有弄清楚问题的代码,任何建议都可以在帮助下考虑。
谢谢
这是我的 Mainfest.xml
<service
android:name=".service.CallService"
android:enabled="true"
android:exported="false" />
这是我的服务班
public class CallService extends Service {
public static final String CHANNEL_ID = "ForegroundServiceChannel";
int startId;
private NotificationPayloadData notificationPayloadData;
public BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null && intent.getExtras() != null) {
String action = intent.getStringExtra(Constant.CALL_RESPONSE_ACTION_KEY);
if (action != null && action.equalsIgnoreCase(Constant.CALL_ACCEPT_ACTION)) {
Intent activityIntent = new Intent(context, VideoCallActivity.class);
activityIntent.setAction("NOTIFICATION_CALL");
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(activityIntent);
if (RingtoneHelper.getRingToneMediaManager(context).isPlaying())
RingtoneHelper.stopRingtoneAndVibrationLoop();
stopForeground();
}
}
}
};
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
this.startId = startId;
notificationPayloadData = intent.getParcelableExtra(Constant.NOTIF_TYPE_DETAILS);
createNotificationChannel();
Intent notificationIntent = new Intent(this.getApplicationContext(), VideoCallActivity.class);
notificationIntent.setAction("NOTIFICATION_CALL");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Intent receiveCallAction = new Intent(Constant.CALL_ACCEPT_REJECT_VALUE);
receiveCallAction.putExtra(Constant.CALL_RESPONSE_ACTION_KEY, Constant.CALL_ACCEPT_ACTION);
PendingIntent receiveCallPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1200, receiveCallAction, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
.setContentTitle("Incoming Voice Call")
.setContentText("From Doctor") //data.getString("remoteUserName")
.setSmallIcon(R.drawable.ic_phone)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setOngoing(true)
.setAutoCancel(false)
.addAction(R.drawable.ic_call_receive, "Receive Call", receiveCallPendingIntent)
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
.build();
registerReceiver(receiver, new IntentFilter(Constant.CALL_ACCEPT_REJECT_VALUE));
startForeground(startId, notification);
if (!RingtoneHelper.getRingToneMediaManager(this).isPlaying())
RingtoneHelper.playRingtoneAndVibrationInLoop(this);
return START_STICKY;
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
"Foreground Service Channel",
NotificationManager.IMPORTANCE_HIGH
);
serviceChannel.setDescription("Call Notifications");
serviceChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void stopForeground() {
Log.d(this.getClass().getSimpleName(), "stopForeground");
stopForeground(true);
stopSelf(startId);
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
}
解决方案
重要的
如果您想进一步自定义频道的默认通知行为,可以调用
enableLights()
、setLightColor()
、setVibrationPattern()
等方法NotificationChannel
。但请记住,一旦您创建了通道,就无法更改这些设置,并且用户可以最终控制这些行为是否处于活动状态。其他选项是卸载并再次安装应用程序。 阅读更多
可能触发提醒通知的条件示例包括:
用户的活动处于全屏模式(应用程序使用 fullScreenIntent)。该通知具有高优先级,并在运行 Android 7.1(API 级别 25)及更低版本的设备上使用铃声或振动。通知通道在运行 Android 8.0(API 级别 26)及更高版本的设备上非常重要。
优先:
Notification.PRIORITY_HIGH
并Notification.PRIORITY_MAX
在 API 级别 26 中被弃用。请改用 NotificationCompat。
这是更多信息:-)
推荐阅读
- excel - 创建复制/粘贴 VBA 循环
- sql-server - 尝试在 SQL Server 2016 中按 XML 列分组时出错
- ios - Xcode Swift Parse Framework 安装问题(命令 PhaseScriptExecution 失败)
- css - 页面上不再显示 Font Awesome 图标
- python - 通过从 Zap B 传递 POST 函数来触发 Zap A
- swift - 如何检查结构的实例本身是否是另一个结构的实例
- powershell - 将变量传递到 sql loader ctrl 文件中
- javascript - 使用两个数组排序
- javascript - 从数组中提取图像索引以便将图像插入引导轮播
- gradle - Gradle 多模块项目未在其中一个模块中运行测试