如何显示弹出通知,例如什么是应用程序调用通知?

时间:2020-11-07 12:19:44

标签: android notifications

我想创建一个弹出式呼叫通知,例如应用程序呼叫通知,但是当我向下滑动通知栏然后看到通知时,通知不会像静态显示在顶部一样。 < / p>

这是我的代码,我没有弄清楚问题,任何建议都可以寻求帮助

谢谢

这是我的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);
}

}

1 个答案:

答案 0 :(得分:0)

重要

如果您想进一步自定义频道的默认通知 行为,您可以调用诸如 enableLights() 之类的方法, setLightColor() setVibrationPattern()上的 NotificationChannel 。 但是请记住,一旦创建频道,就无法更改这些频道 设置,用户可以最终控制这些行为是否 活性。另一种选择是再次卸载并安装应用程序。 Read more

可能触发平视通知的条件示例 包括:

用户的活动处于全屏模式(应用使用 fullScreenIntent)。通知具有较高的优先级并使用 运行Android 7.1(API级别25)的设备上的铃声或振动 和更低。通知通道在设备上非常重要 运行Android 8.0(API级别26)及更高版本。

优先级:

Notification.PRIORITY_HIGHNotification.PRIORITY_MAX是 API级别26中已弃用。改为使用NotificationCompat。

Here是更多信息:-)