Android 10来电通知,例如当我们在另一个应用程序中时是什么应用程序 [英] Android 10 Incoming call notification like whats app when we are in another app

查看:182
本文介绍了Android 10来电通知,例如当我们在另一个应用程序中时是什么应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一旦我们收到了限制的android 10后台启动活动中的FCM推送通知消息.当我们在另一个应用程序中时,需要像WhatsApp和Skype通知来电这样的解决方案.

Once we received FCM push notification message in android 10 background start activities restricted. Need a solution like WhatsApp and Skype notification incoming call when we are in another app.

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

任何帮助将不胜感激.

用于前台服务和对时间敏感的通知的以下代码.

below code for foreground service and a time-sensitive notification.

startForeground(1234,getNotification(incomingCallIntent));

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


}

推荐答案

我找到了解决方案.您需要为此创建一个前台服务. 这也将帮助您在锁定屏幕中显示振铃拨号器.您需要添加Mediaplayer才能播放铃声.

I find a solution for that. You need to create a foreground service for that. This will also help you to show ringing dialer in lock screen also. you need to add mediaplayer for play ringtone.

CallNotificationService

public class HeadsUpNotificationService extends Service  {
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";


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

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

}

服务接收者

CallNotificationActionReceiver

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

我们需要在应用程序标签的清单中设置这两个

We need set this two in manifest inside application tab

清单

  <!-- 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通知服务中调用它.

To start the service you need to call it from your firebase notification service.

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

这篇关于Android 10来电通知,例如当我们在另一个应用程序中时是什么应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆