Android 10在另一个应用程序中时,来电通知类似于WhatsApp

15

当我们在Android 10后台收到FCM推送通知消息时,启动活动受到限制。需要像WhatsApp和Skype通知那样,在我们使用其他应用程序时接收来电通知的解决方案。

输入图像描述

int NOTIFICATIONID = 1234;
       // Uri sound =  RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.capv_callingtone);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .build();
            String CHANNEL_ID = BuildConfig.APPLICATION_ID.concat("_notification_id");
            String CHANNEL_NAME = BuildConfig.APPLICATION_ID.concat("_notification_name");
            assert notificationManager != null;

            NotificationChannel mChannel = notificationManager.getNotificationChannel(CHANNEL_ID);
            if (mChannel == null) {
                mChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
                mChannel.setSound(sound, audioAttributes);
                notificationManager.createNotificationChannel(mChannel);
            }
            in.setClass(CapVFirebaseMessagingService.this, DashBoardActivity.class);
            in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            in.putExtra(NOTIFICATION_ID, NOTIFICATIONID);
            Intent buttonIntent = new Intent(getBaseContext(), NotificationReceiver.class);
            buttonIntent.putExtra(NOTIFICATION_ID, NOTIFICATIONID);
            buttonIntent.putExtra(CapV.MESSAGE_TYPE,in.getSerializableExtra(CapV.MESSAGE_TYPE));
            Log.d("Audiotask",""+in.getSerializableExtra(CapV.MESSAGE_TYPE));
            PendingIntent dismissIntent = PendingIntent.getBroadcast(getBaseContext(), 0, buttonIntent, 0);
            SharedPreferences localPrefs = getSharedPreferences(LOCAL_PREFERENCES,MODE_PRIVATE);
            SharedPreferences.Editor editor = getSharedPreferences(LOCAL_PREFERENCES, MODE_PRIVATE).edit();
            editor.putBoolean("Fragment_created",true).commit();
            editor.putBoolean("Incoming_call",true).commit();
            // The PendingIntent to launch activity.
            PendingIntent activityPendingIntent = PendingIntent.getActivity(this, 0,
                    in, 0);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);

            builder.setSmallIcon(R.drawable.logo)
                    .setContentTitle(("Incoming Call"))
                    .setContentText("Group")
                    .setDefaults(0)
                    .addAction(R.drawable.answer, getString(R.string.answer),
                            activityPendingIntent)
                    .addAction(R.drawable.reject, getString(R.string.reject),
                            dismissIntent)
                    .setPriority(NotificationCompat.PRIORITY_MAX)
                    .setCategory(NotificationCompat.CATEGORY_CALL)
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                    .setSound(sound)
                    .setOngoing(true);
            android.app.Notification notification = builder.build();
            notificationManager.notify(1234, notification);
任何帮助都将不胜感激。
以下代码用于前台服务和一个时间敏感的通知。
startForeground(1234, getNotification(incomingCallIntent));
private android.app.Notification getNotification(Intent in) {



    in.putExtra(EXTRA_STARTED_FROM_NOTIFICATION, true);

   PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,in, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this,"")
            .setSmallIcon(R.drawable.logo)
            .setAutoCancel(true)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setFullScreenIntent(pendingIntent, true);

    // Set the Channel ID for Android O.
        //builder.setChannelId("115"); // Channel ID

   return builder.build();


}

1
这个回答是否解决了你的问题?如何在安卓10中打开活动(VoIP呼入) - Grisgram
有人能帮我解决这个问题吗? - user1847921
你有任何解决方案吗? - SARATH V
你找到了解决方案吗? - Mbula Mboma Jean gilbert
https://dev59.com/Pqrka4cB1Zd3GeqPfo8L#49793287 - Krishna Mer
显示剩余3条评论
2个回答

18

我找到了解决方法。你需要为此创建前台服务。 这也将帮助你在锁屏界面上显示响铃电话。 在这个代码中,我添加了铃声和振动。

CallNotificationService

public class HeadsUpNotificationService extends Service implements MediaPlayer.OnPreparedListener  {
private String CHANNEL_ID = AppController.getInstance().getContext().getString(R.string.app_name)+"CallChannel";
private String CHANNEL_NAME = AppController.getInstance().getContext().getString(R.string.app_name)+"Call Channel";MediaPlayer mediaPlayer;
Vibrator mvibrator;
AudioManager audioManager;
AudioAttributes  playbackAttributes;
private Handler handler;
AudioManager.OnAudioFocusChangeListener afChangeListener;
private boolean status = false;
private boolean vstatus = false;


@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Bundle data = null;
    String name="",callType="";
    int NOTIFICATION_ID=120;try {
        audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

        if (audioManager != null) {
            switch (audioManager.getRingerMode()) {
                case AudioManager.RINGER_MODE_NORMAL:
                    status = true;
                    break;
                case AudioManager.RINGER_MODE_SILENT:
                    status = false;
                    break;
                case AudioManager.RINGER_MODE_VIBRATE:
                    status = false;
                    vstatus=true;
                    Log.e("Service!!", "vibrate mode");
                    break;
            }
        }

        if (status) {
            Runnable delayedStopRunnable = new Runnable() {
                @Override
                public void run() {
                    releaseMediaPlayer();
                }
            };

            afChangeListener =  new AudioManager.OnAudioFocusChangeListener() {
               public void onAudioFocusChange(int focusChange) {
                   if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
                       // Permanent loss of audio focus
                       // Pause playback immediately
                       //mediaController.getTransportControls().pause();
                       if (mediaPlayer!=null) {
                           if (mediaPlayer.isPlaying()) {
                               mediaPlayer.pause();
                           }
                       }
                       // Wait 30 seconds before stopping playback
                       handler.postDelayed(delayedStopRunnable,
                               TimeUnit.SECONDS.toMillis(30));
                   }
                   else if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
                       // Pause playback
                   } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
                       // Lower the volume, keep playing
                   } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
                       // Your app has been granted audio focus again
                       // Raise volume to normal, restart playback if necessary
                   }
               }
           };
             KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);


            mediaPlayer= MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI);
            mediaPlayer.setLooping(true);
            //mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                 handler = new Handler();


              playbackAttributes = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
                    .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                    .build();

                AudioFocusRequest focusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT)
                        .setAudioAttributes(playbackAttributes)
                        .setAcceptsDelayedFocusGain(true)
                        .setOnAudioFocusChangeListener(afChangeListener, handler)
                        .build();
                int res = audioManager.requestAudioFocus(focusRequest);
                if (res == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                      if(!keyguardManager.isDeviceLocked()) {

                          mediaPlayer.start();
                         }

                }
            }else {

                // Request audio focus for playback
                int result = audioManager.requestAudioFocus(afChangeListener,
                        // Use the music stream.
                        AudioManager.STREAM_MUSIC,
                        // Request permanent focus.
                        AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);

                if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                    if(!keyguardManager.isDeviceLocked()) {
                        // Start playback
                        mediaPlayer.start();
                    }
                }

            }

        }
        else if(vstatus){
            mvibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            // Start without a delay
            // Each element then alternates between vibrate, sleep, vibrate, sleep...
            long[] pattern = {0, 250, 200, 250, 150, 150, 75,
                    150, 75, 150};

            // The '-1' here means to vibrate once, as '-1' is out of bounds in the pattern array
            mvibrator.vibrate(pattern,0);
            Log.e("Service!!", "vibrate mode start");

        }

    } catch (Exception e) {
        e.printStackTrace();
    }         
         
    if (intent != null && intent.getExtras() != null) {
       
        data = intent.getExtras();
        name =data.getString("inititator");
        if(AppController.getInstance().getCall_type().equalsIgnoreCase(ApplicationRef.Constants.AUDIO_CALL)){
            callType ="Audio";
        }
        else {
            callType ="Video";
        }

    }
    try {
        Intent receiveCallAction = new Intent(AppController.getInstance().getContext(), CallNotificationActionReceiver.class);

        receiveCallAction.putExtra("ConstantApp.CALL_RESPONSE_ACTION_KEY", "ConstantApp.CALL_RECEIVE_ACTION");
        receiveCallAction.putExtra("ACTION_TYPE", "RECEIVE_CALL");
        receiveCallAction.putExtra("NOTIFICATION_ID",NOTIFICATION_ID);
        receiveCallAction.setAction("RECEIVE_CALL");

        Intent cancelCallAction = new Intent(AppController.getInstance().getContext(), CallNotificationActionReceiver.class);
        cancelCallAction.putExtra("ConstantApp.CALL_RESPONSE_ACTION_KEY", "ConstantApp.CALL_CANCEL_ACTION");
        cancelCallAction.putExtra("ACTION_TYPE", "CANCEL_CALL");
        cancelCallAction.putExtra("NOTIFICATION_ID",NOTIFICATION_ID);
        cancelCallAction.setAction("CANCEL_CALL");

        Intent callDialogAction = new Intent(AppController.getInstance().getContext(), CallNotificationActionReceiver.class);
        callDialogAction.putExtra("ACTION_TYPE", "DIALOG_CALL");
        callDialogAction.putExtra("NOTIFICATION_ID",NOTIFICATION_ID);
        callDialogAction.setAction("DIALOG_CALL");

        PendingIntent receiveCallPendingIntent = PendingIntent.getBroadcast(AppController.getInstance().getContext(), 1200, receiveCallAction, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent cancelCallPendingIntent = PendingIntent.getBroadcast(AppController.getInstance().getContext(), 1201, cancelCallAction, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent callDialogPendingIntent = PendingIntent.getBroadcast(AppController.getInstance().getContext(), 1202, callDialogAction, PendingIntent.FLAG_UPDATE_CURRENT);

        createChannel();
        NotificationCompat.Builder notificationBuilder = null;
        if (data != null) {
           // Uri ringUri= Settings.System.DEFAULT_RINGTONE_URI;
            notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setContentTitle(name)
                    .setContentText("Incoming "+callType+" Call")
                    .setSmallIcon(R.drawable.ic_call_icon)
                    .setPriority(NotificationCompat.PRIORITY_MAX)
                    .setCategory(NotificationCompat.CATEGORY_CALL)
                    .addAction(R.drawable.ic_call_decline, getString(R.string.reject_call), cancelCallPendingIntent)
                    .addAction(R.drawable.ic_call_accept, getString(R.string.answer_call), receiveCallPendingIntent)
                    .setAutoCancel(true)
                    //.setSound(ringUri)
                    .setFullScreenIntent(callDialogPendingIntent, true);
            
        }

        Notification incomingCallNotification = null;
        if (notificationBuilder != null) {
            incomingCallNotification = notificationBuilder.build();
        }
        startForeground(NOTIFICATION_ID, incomingCallNotification);

       
    } catch (Exception e) {
        e.printStackTrace();
    }

    return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();// release your media player here audioManager.abandonAudioFocus(afChangeListener);
    releaseMediaPlayer();
    releaseVibration();
}

public void createChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        try {
            Uri ringUri= Settings.System.DEFAULT_RINGTONE_URI;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
            channel.setDescription("Call Notifications");
            channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
           /* channel.setSound(ringUri,
                    new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                            .setLegacyStreamType(AudioManager.STREAM_RING)
                            .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION).build());*/
            Objects.requireNonNull(AppController.getInstance().getContext().getSystemService(NotificationManager.class)).createNotificationChannel(channel);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}public void releaseVibration(){
    try {
        if(mvibrator!=null){
            if (mvibrator.hasVibrator()) {
                mvibrator.cancel();
            }
            mvibrator=null;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
private void releaseMediaPlayer() {
    try {
        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
                mediaPlayer.reset();
                mediaPlayer.release();
            }
            mediaPlayer = null;
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public void onPrepared(MediaPlayer mediaPlayer) {

}}

服务接收器

呼叫通知操作接收器

public class CallNotificationActionReceiver extends BroadcastReceiver {


Context mContext;

@Override
public void onReceive(Context context, Intent intent) {
    this.mContext=context;
    if (intent != null && intent.getExtras() != null) {
      
        String action ="";
        action=intent.getStringExtra("ACTION_TYPE");

        if (action != null&& !action.equalsIgnoreCase("")) {
            performClickAction(context, action);
        }

        // Close the notification after the click action is performed.
        Intent iclose = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        context.sendBroadcast(iclose);
        context.stopService(new Intent(context, CallNotificationService.class));

    }


}
private void performClickAction(Context context, String action) {
    if(action.equalsIgnoreCase("RECEIVE_CALL")) {

        if (checkAppPermissions()) {                
        Intent intentCallReceive = new Intent(mContext, VideoCallActivity.class);
        intentCallReceive.putExtra("Call", "incoming");
            intentCallReceive.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            mContext.startActivity(intentCallReceive);               
        }
        else{
                    Intent intent = new Intent(AppController.getInstance().getContext(), VideoCallRingingActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.putExtra("CallFrom","call from push");
                    mContext.startActivity(intent);                  
            
        }
    }
    else if(action.equalsIgnoreCase("DIALOG_CALL")){

                // show ringing activity when phone is locked
                Intent intent = new Intent(AppController.getInstance().getContext(), VideoCallRingingActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                mContext.startActivity(intent);
            }

    else {            
        context.stopService(new Intent(context, CallNotificationService.class));
        Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        context.sendBroadcast(it);
    }
}

private Boolean checkAppPermissions() {
    return hasReadPermissions() && hasWritePermissions() && hasCameraPermissions() && hasAudioPermissions();
}

private boolean hasAudioPermissions() {
    return (ContextCompat.checkSelfPermission(AppController.getInstance().getContext(), Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED);
}

private boolean hasReadPermissions() {
    return (ContextCompat.checkSelfPermission(AppController.getInstance().getContext(), Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
}

private boolean hasWritePermissions() {
    return (ContextCompat.checkSelfPermission(AppController.getInstance().getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
}
private boolean hasCameraPermissions() {
    return (ContextCompat.checkSelfPermission(AppController.getInstance().getContext(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED);
}
}

我们需要在应用程序选项卡中的清单中设置这两个内容。

清单

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT"/>
<uses-permission android:name="android.permission.VIBRATE" />
  <!-- Incoming call  -->
    <service android:name=".CallNotificationService"/>
    <receiver
        android:name=".CallNotificationActionReceiver"
        android:enabled="true">
        <intent-filter android:priority="999">
            <action android:name="ConstantApp.CALL_RECEIVE_ACTION" />
            <action android:name="ConstantApp.CALL_CANCEL_ACTION"/>
        </intent-filter>
    </receiver>

要启动此服务,您需要从您的Firebase通知服务中调用它。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    Intent serviceIntent = new Intent(getApplicationContext(), CallNotificationService.class);
                                    Bundle mBundle = new Bundle();
                                    mBundle.putString("inititator", name);
                                    mBundle.putString("call_type",callType);
                                    serviceIntent.putExtras(mBundle);
                                    ContextCompat.startForegroundService(getApplicationContext(), serviceIntent);
}

停止通知

getApplicationContext().stopService(new Intent(AppController.getInstance().getContext(), HeadsUpNotificationService.class));
                                    Intent istop = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
                                    getApplicationContext().sendBroadcast(istop);

2
非常感谢这个。但是我遇到了一个问题,当应用程序在后台时服务没有启动。 - Adesuyi Ayodeji
1
在启动广播意图时添加标志:(FLAG_ACTIVITY_BROUGHT_TO_FRONT,FLAG_ACTIVITY_REORDER_TO_FRONT,FLAG_FROM_BACKGROUND),并在屏幕锁定时在启动活动中添加设置。同时,在清单权限中添加WAKE_LOCK。https://dev59.com/uWIj5IYBdhLWcg3wk142#55901820 - Adam
感谢提供参考代码...我已经能够显示来电通知,但我的通知横幅不能持续超过5秒钟。是否有任何优雅的方式来处理这个问题,就像WhatsApp在接收到来电时所做的那样? - Nik
@Ibrahim117 对我来说,它的工作效果很好。我的通知会一直保留,直到被取消。 - Dinil ps
@Sabarinathan 在服务的 onDestroy() 方法中释放 MediaPlayer 并将其设置为 null。请检查我的代码。 - Dinil ps
显示剩余14条评论

2
这可以通过来自 FCM 的数据消息实现。您需要设置以下内容:
  1. 创建一个扩展 FirebaseMessagingService 的服务。在 FCM 文档中参考此处。我们将在服务类中重写 onMessageReceived()。
  2. 创建一个全屏意图或显示时间敏感通知。在 FCM 文档中参考此处。在我们的服务中,覆盖 onMessageReceived() 并显示此通知。
  3. 现在,您必须找到一种方法来从 FCM 发送数据消息。Firebase 控制台不支持此操作。后端必须为此创建一个条款。在此处参考此处
让我深入了解更多细节。 我们在 FCM 中有两种不同的消息类型
  1. 通知消息:这是常见的消息类型。这可以从 FCM 控制台中的通知合成器发送。这种类型的消息将具有名为标题、正文等的属性。如果我们发送通知消息,则只有当应用程序在前台时才会调用 onMessageReceived()。
  2. 数据消息:这不能从 FCM 控制台的通知合成器发送。此消息类型应具有数据(有效负载)属性。这是从服务器发送的,可以是以下格式:
{
    "data":{
        "property1":"value1",
        "property2":42
    }
}
// Please note it should have data node.

如果我们发送数据消息,即使应用程序在后台或不在内存中,onMessageReceived() 将被调用。这正是我们想要的!
请参阅 FCM 文档中的数据消息

让我重新梳理一下步骤
步骤 #1:这是我们唯一需要创建的服务。我们不需要创建任何其他前台服务。这是一个不同的场景。重写 onMessageReceived() 来执行第二步。

步骤 #2:全屏意图或显示时间敏感通知将显示类似于问题中所示的弹出式通知,或者整个活动以显示呼入电话。当我们接收到第三步中提到的数据消息时,在 onMessageReceived() 中显示此通知。

步骤 #3:联系服务器以发送上面显示的有效负载 JSON。

就是这样!


@Dinil 感谢您提供的答案,它适用于 Android 11 及以下版本。但在 Android 12 及更高版本上无法正常工作,请帮忙解决一下。 - user1847921

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接