Android — 在服务中发送通知Notification

1.在服务中,发送通知的方法

  private void sendNotification(String title,String content,StateBean stateBean){
        Context context=getApplicationContext();
        String channelId = "通知消息";//和application通道的channedId名字要一样
        //设置TaskStackBuilder,点击通知栏跳转到指定页面,点击返回。返回到的页面
//        Intent returnIntent = new Intent(this, HomeActivity.class);
//        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
//        stackBuilder.addParentStack(NotificationIntent.class);
//        stackBuilder.addNextIntent(returnIntent);
        //设置点击状态栏跳转的地方,详情页
        Intent intent = new Intent(context, DetailActivity.class);
        intent.putExtra("bean",stateBean);


        //解决PendingIntent的extra数据不准确问题
        //注意下面的最后一个参数用PendingIntent.FLAG_UPDATE_CURRENT,否则传参有问题
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        //通知管理类
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //设置通知属性,创建通知build,(创建channel通道已经在MApplication里面声明了)

//使用默认的震动
        Notification notification = new NotificationCompat.Builder(context,channelId)
                .setContentTitle(title)//设置标题
                .setContentText(content)//消息内容
                .setWhen(System.currentTimeMillis())//发送时间
                .setSmallIcon(R.mipmap.logo) //设置图标
                .setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE)//设置默认的提示音,振动方式,灯光
                .setContentIntent(pendingIntent)//传值跳转的内容
                .setAutoCancel(true)//点击通知时是否自动消失,需要重点注意的是,setAutoCancel需要和setContentIntent一起使用,否则无效。
//                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.app_icon))
//                .setFullScreenIntent(pendingIntent,true)//悬挂式通知栏。但是在oppo8.0试了。通知会自动跳到intent的页面。网上说sdk设为29就没事了。android10.0测了不会自己跳转
                .build();
        notification.defaults = Notification.DEFAULT_ALL ;//设置为默认的声音
        //发送通知
        manager.notify(123, notification);
    }

2.要在MApplication中创建发送通知的通道

 /**
     * 创建通道
     */
    private void createChannel(){
        NotificationManager  mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // 通知渠道的id
        String channelId = "通知消息";
        // 用户可以看到的通知渠道的名字.
        CharSequence name = "通知消息的通道";
        // 用户可以看到的通知渠道的描述
        //        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel channel = null;
        //Android8.0要求设置通知渠道
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            channel = new NotificationChannel(channelId,name, NotificationManager.IMPORTANCE_HIGH);
            // 配置通知渠道的属性
            //        mChannel.setDescription(description);
            // 设置通知出现时的闪灯(如果 android 设备支持的话)
            channel.enableLights(true);
            channel.setLightColor(Color.RED);
            // 设置通知出现时的震动(如果 android 设备支持的话)
            channel.enableVibration(true);
            channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            //最后在notificationmanager中创建该通知渠道
            mNotificationManager.createNotificationChannel(channel);
        }

    }

注:发送通知的ChannelId要和 MApplication创建的通道ChannelId 要一样

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android,可以使用TextToSpeech(TTS)引擎将文字转换为语音。要在Notification发送通知时使用TTS读出通知内容,可以使用以下步骤: 1. 在AndroidManifest.xml文件添加以下权限: ```xml <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> ``` 2. 在build.gradle文件添加以下依赖项: ```groovy dependencies { implementation 'com.android.support:support-v4:28.0.0' implementation 'com.google.android.gms:play-services:12.0.1' } ``` 3. 在Notification发送通知时,调用TextToSpeech引擎将通知内容转换为语音,例如: ```java NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!") .setPriority(NotificationCompat.PRIORITY_HIGH); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(notificationId, builder.build()); TextToSpeech tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() { @Override public void onInit(int status) { if (status == TextToSpeech.SUCCESS) { tts.setLanguage(Locale.getDefault()); tts.speak("Hello World!", TextToSpeech.QUEUE_ADD, null); } } }); ``` 这将发送一个通知并将“Hello World!”转换为语音。请注意,上述代码只是示例,您需要根据您的需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值