简体   繁体   English

如何显示弹出通知,例如什么是应用呼叫通知?

[英]How to show pop-up notification like what's app call notification?

I want to create a pop-up call notification like what's app call notification, but my notification not showing on top like statically, when I slide down the notification bar then I see the notification.我想创建一个弹出式呼叫通知,比如什么是应用程序呼叫通知,但我的通知没有像静态那样显示在顶部,当我向下滑动通知栏时,我看到了通知。

Here is my code I didn't figure out the issue, Any suggestion considers with help .这是我的代码,我没有弄清楚问题,任何建议都在帮助下考虑

Thank you谢谢

This is my Mainfest.xml这是我的 Mainfest.xml

<service
        android:name=".service.CallService"
        android:enabled="true"
        android:exported="false" />

Here is my service class这是我的服务类

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);
}

} }

Important重要的

If you'd like to further customize your channel's default notification behaviors, you can call methods such as enableLights() , setLightColor() , and setVibrationPattern() on the NotificationChannel .如果您想进一步自定义频道的默认通知的行为,你可以调用的方法,如enableLights() setLightColor()setVibrationPattern()NotificationChannel But remember that once you create the channel, you cannot change these settings and the user has final control of whether these behaviors are active.但请记住,一旦您创建了频道,就无法更改这些设置,并且用户对这些行为是否处于活动状态拥有最终控制权。 Other option is to uninstall and install application again.另一个选项是卸载并重新安装应用程序。 Read more 阅读更多

Examples of conditions that may trigger heads-up notifications include:可能触发提醒通知的条件示例包括:

The user's activity is in fullscreen mode (the app uses fullScreenIntent).用户的活动处于全屏模式(应用程序使用 fullScreenIntent)。 The notification has high priority and uses ringtones or vibrations on devices running Android 7.1 (API level 25) and lower.该通知具有高优先级,并在运行 Android 7.1(API 级别 25)及更低版本的设备上使用铃声或振动。 The notification channel has high importance on devices running Android 8.0 (API level 26) and higher.通知渠道对于运行 Android 8.0(API 级别 26)及更高版本的设备非常重要。

Priority:优先事项:

Notification.PRIORITY_HIGH and Notification.PRIORITY_MAX was deprecated in API level 26. use NotificationCompat instead. Notification.PRIORITY_HIGHNotification.PRIORITY_MAX在 API 级别 26 中已弃用。请改用 NotificationCompat。

Here is more info :-) 是更多信息:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM