全屏通知仅显示为抬头

Full Screen Notification only showing as a Heads Up

我已经为这个问题苦苦思索 table 3 天了,请告诉我我误入歧途的地方。

当我收到 VoIP 来电时,我试图显示全屏通知,就像 PhoneApp 一样。如果用户身临其境 activity,我可以提个醒。但是,我只收到提醒通知,而从未获得全屏。我也需要通知忽略锁屏。

这是我正在做的事情:

String callJson = call.toJson(uber).toString();
uber.requestWakeState(UberVoice.WAKE_STATE_FULL);

final Notification.Builder builder = new Notification.Builder(uber);
builder.setSmallIcon(R.drawable.switch_notification);
if (image != null) {
    builder.setLargeIcon(image);
}
builder.setOngoing(true);

PendingIntent inCallPendingIntent =
        PendingIntent.getActivity(uber, 234,
                UberVoice.createInCallIntent(), 0);
builder.setContentIntent(inCallPendingIntent);

builder.setContentText(body);
builder.setUsesChronometer(false);

builder.setContentTitle(title);

builder.setCategory(Notification.CATEGORY_CALL);
builder.setVisibility(Notification.VISIBILITY_PUBLIC);

builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE), AudioManager.STREAM_RING);
builder.setVibrate(new long[]{500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500});

builder.setPriority(Notification.PRIORITY_MAX);
builder.setTicker(title);
builder.setFullScreenIntent(inCallPendingIntent, true);

builder.addAction(R.drawable.hangup_action, "Reject", CallService.getPendingIntent(uber, callJson, CallService.REJECT));
builder.addAction(R.drawable.answer_action, "Answer", CallService.getPendingIntent(uber, callJson, CallService.ANSWER));

NotificationManager notificationManager = (NotificationManager) uber.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = builder.build();
notificationManager.notify(INCOMING_CALL_NOTIFICATION_ID, notification);

这是 createInCallIntent 代码:

public static Intent createInCallIntent() {
    Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
            | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
    intent.setClassName("<package>", IncomingCallActivity.class.getName());
    return intent;
}

这里是 IncomingCallActivity.onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    int flags = WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
            | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;

    getWindow().addFlags(flags);

    setContentView(R.layout.activity_incoming_call);

我试过直接启动这个activity(uber是应用参考)

Intent incomingCallIntent = new Intent(uber, IncomingCallActivity.class);
String callJson = call.toJson(uber).toString();
incomingCallIntent.putExtra("call", callJson);

incomingCallIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

uber.startActivity(incomingCallIntent);

我做错了什么?

如果我没有正确理解你的问题,你忘记在 IncomingCallActivity

中添加标记 WindowManager.LayoutParams.FLAG_FULLSCREEN
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    int flags = WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
            | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
            | WindowManager.LayoutParams.FLAG_FULLSCREEN;

    getWindow().addFlags(flags);

此组合需要在屏幕锁定时显示 activity。 希望对你有帮助。

想通了!正如我所怀疑的那样,这是愚蠢的事情。

在我的 IncomingCallActivity 中,我有以下代码

public void onPause() {
    super.onPause();
    finish();
}

事实证明,与通知显示方式有关的事情实际上调用了 onPause,因此完成了 activity。

感谢@JohanShogun 的帮助。

//in android 10 a permssion is used try it
 
 <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT"/>