未接电话通知之多条通知

    技术2022-07-21  158

    未接电话通知之多条通知

    公司最近在开发一款基于linphone的sip电话只需要其中的电话功能 在来电时未接的情况下能够通过通知栏显示出来不同的号码用不同的通知显示 相同的号码显示未接的数量

    private void createDeclinedNotification(String phoneNum) { Intent notificationIntent; notificationIntent= new Intent(this, DialerActivity.class); //未接的电话号码 notificationIntent.putExtra("call",phoneNum); //未接来电数量 int number=0; for(int i=0;i<dismissPhoneNums.size();i++){ if(phoneNum.equals(dismissPhoneNums.get(i))){ number++; } } String contextText; if(number>1){ contextText="未接电话"+"("+number+")"; }else { contextText="未接电话"; } PendingIntent pendingIntent = PendingIntent.getActivity(this, notificationId, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); String CHANNEL_ONE_ID = getPackageName(); String CHANNEL_ONE_NAME = "Channel One"; NotificationChannel notificationChannel = null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { notificationChannel = new NotificationChannel(CHANNEL_ONE_ID, CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_HIGH); notificationChannel.enableLights(true); notificationChannel.setLightColor(Color.RED); notificationChannel.setShowBadge(true); notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.createNotificationChannel(notificationChannel); notification = new Notification.Builder(this, CHANNEL_ONE_ID) .setSmallIcon(R.drawable.ic_launcher_nightly) .setWhen(System.currentTimeMillis()) .setTicker("WES") .setContentTitle(phoneNum) .setContentText(contextText) .setShowWhen(true) //.setDefaults(Notification.DEFAULT_ALL)//给通知添加声音、闪光灯和震动效果,不过单独设置震动效果需要添加系统权限 .setContentIntent(pendingIntent) .setAutoCancel(true) .build(); } else { notification = new Notification.Builder(this) .setSmallIcon(R.drawable.ic_launcher_nightly) .setWhen(System.currentTimeMillis()) .setTicker("WES") .setContentTitle(phoneNum) .setContentText(contextText) .setShowWhen(true) //.setDefaults(Notification.DEFAULT_ALL)//给通知添加声音、闪光灯和震动效果,不过单独设置震动效果需要添加系统权限 .setContentIntent(pendingIntent) .setAutoCancel(true) .build(); } manager.notify(notificationId,notification); }

    在实现此功能的过程中 也遇到了不少问题 比如该如何实现不同电话的未接通知 通过查找别人的博客知道可以修改 manager.notify(notificationId,notification)和PendingIntent.getActivity(this, notificationId, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT)中的notificationId达到多条通知

    PendingInteng.getBroadcast(contex, requestCode, intent, flags) PendingInteng.getService(contex, requestCode, intent, flags) PendingInteng.getActivity(contex, requestCode, intent, flags) PendingInteng.getActivities(contex, requestCode, intent, flags)

    其中flags属性参数用于确定PendingIntent的行为: FLAG_ONE_SHOT: 表示返回的PendingIntent仅能执行一次,执行完后自动消失? FLAG_NO_CREATE: 表示如果描述的PendingIntent不存在,并不创建相应的PendingIntent,而是返回NULL FLAG_CANCEL_CURRENT: 表示相应的PendingIntent已经存在,则取消前者,然后创建新的PendingIntent FLAG_UPDATE_CURRENT: 表示更新的PendingIntent,如果构建的PendingIntent已经存在,则替换它,常用

    还有点击通知栏跳转到拨号界面 并绑定未接的电话号码的功能 最初想用SharedPreference实现 发现不可行就改用putExtra的方法 将未接的电话号码存放于intent中 然后测试的时候发现拨号界面每次都只显示其中一个未接的号码 而点击另一个通知栏拨号界面就不绑定未接号码 又通过调试发现第二次点击通知栏 因为已经处于拨号界面 所以不会重新创建activity onCreat() onStart() onResume()方法在第二次点击通知栏的时候都没有调用 只有finish掉拨号界面在点击第二条通知栏才会绑定数据 上图

    Processed: 0.009, SQL: 9