android的消息提示功能

android中发送消息notification点击跳转到指定的界面

发送消息功能

背景:android是用来给原来的web程序打包用的,就是原来的网页程序使用webview打包成一个移动端app。

主要的功能函数:

这个notice()方法是用来给网页的前端调用的,主要是使用注解@JavascriptInterface实现的。

  /**
     * 调用消息提示的功能(供页面前端js调用)
     * @param message 服务器发送过来的消息(消息内容+taskId)
     */
    @JavascriptInterface
    public void notice(String message) {
        String me=message.substring(0,message.indexOf("|"));
        String path=message.substring(message.indexOf("|")+1,message.length());
        handleNotification(me,path);
    }

消息提示的功能:

实现结果:发送消息后在消息栏显示消息信息,多条消息不合并显示,点击不同的消息跳转到不同的页面。

getActivity不会打开新的activity:设置activity只能存在一个

android:launchMode="singleTop"
    /**
     * 消息提示功能
     * @param message 传递的消息的具体内容
     * @param path 传递的任务的id
     */
    private void handleNotification(String message,String path) {
        Intent intent = new Intent();
        intent.setClass(this, MainActivity.class);
        intent.putExtra("pushMsg",path);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, getAnId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);

        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationChannel notificationChannel = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            notificationChannel = new NotificationChannel("notification", "测试通知", NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(notificationChannel);
        }

        // 创建通知
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "notification")
                .setSmallIcon(R.drawable.notification)
                .setWhen(System.currentTimeMillis())
                .setContentIntent(pendingIntent)
                .setContentTitle("消息提示")
                .setGroupSummary(false)//避免消息合并的
                .setGroup("group")//避免消息合并的
                .setContentText("你有一个新的任务:" + message + ",请及时处理!")
                .setPriority(NotificationCompat.PRIORITY_MAX);

        // 发送通知
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
   			//获取权限,如果已经在xml中给定了权限可以不用写这一部分
            return;
        }
        notificationManager.notify(getAnId(), builder.build());
    }

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值