项目是即时通讯项目,想做一个类似与qq,微信程序在后台,收到消息后在顶部弹出的效果,行话叫做heads up ,哈哈,我也是道听途说而已,听不懂的东西就感觉很牛逼了,

好了,不扯淡了,今天写一下遇到的这个通知横幅的问题。

     当然这个问题得限制在android5.0系统以上使用了

主要代码贴一下:

mNotificationIntent = new Intent(this, MainActivity.class);// 启动主界面
        String password = PreferenceUtils.getPrefString(this,
                PreferenceConstants.PASSWORD, "");// 取密码
        if (TextUtils.isEmpty(password)) {
            mNotificationIntent.setClass(this, LoginActivity.class);
        }
        int mNotificationCounter = 0;
        if (mNotificationCount.containsKey(comeFromId)) {
            mNotificationCounter = mNotificationCount.get(comeFromId);
        }
        mNotificationCounter++;
        mNotificationCount.put(comeFromId, mNotificationCounter);
        String author = fromUserId;
        String title = author;
        String ticker;
        boolean isTicker = PreferenceUtils.getPrefBoolean(this,
                PreferenceConstants.TICKER, true);
        if (isTicker) {
            if (message == null) {
                L.e("BaseService 错误信息 : message = " + message);
                return;
            }
            int newline = message.indexOf('\n');
            int limit = 0;
            String messageSummary = message;
            if (newline >= 0)
                limit = newline;
            if (limit > MAX_TICKER_MSG_LEN
                    || message.length() > MAX_TICKER_MSG_LEN)
                limit = MAX_TICKER_MSG_LEN;
            if (limit > 0)
                messageSummary = message.substring(0, limit) + " [...]";
            ticker = title + ":\n" + messageSummary;
        } else
            ticker = author;
        // mNotification = new Notification(R.drawable.ef_notify, ticker,
        // sendTime);
        Uri userNameUri = Uri.parse(comeFromId);
        mNotificationIntent.setData(userNameUri);
        mNotificationIntent.putExtra("isNotifyIntent", true);
        mNotificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        // need to set flag FLAG_UPDATE_CURRENT to get extras transferred
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 1,// requestCode是0的时候三星手机点击通知栏通知不起作用
                mNotificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent pendingIntent1 = PendingIntent.getActivity(this, 1,// requestCode是0的时候三星手机点击通知栏通知不起作用
                new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
        Builder notificationBuilder = new Notification.Builder(this);

        notificationBuilder
                .setSmallIcon(R.drawable.logo_notify)
                // 设置状态栏中的小图片,尺寸一般建议在24×24,这个图片同样也是在下拉状态栏中所显示,如果在那里需要更换更大的图片,可以使用setLargeIcon(Bitmap
                // icon)
                .setTicker(ticker)
                // 设置在status
                // bar上显示的提示文字
                .setContentTitle(title)
                // 设置在下拉status
                // bar后Activity,本例子中的NotififyMessage的TextView中显示的标题
                .setContentText(message)
                // TextView中显示的详细内容
                .setContentIntent(pendingIntent)
                .setNumber(mNotificationCounter)
                // 在TextView的右方显示的数字,可放大图片看,在最右侧。这个number同时也起到一个序列号的左右,如果多个触发多个通知(同一ID),可以指定显示哪一个。
                .setAutoCancel(true); // 需要注意build()是在API
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            notificationBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
            // 关联PendingIntent
            notificationBuilder.setFullScreenIntent(pendingIntent1, false);// 横幅
        }

        mNotification = notificationBuilder.getNotification(); // level
        mNotification.contentIntent = pendingIntent;



   字号比较大的地方,就是显示横幅通知的重点了,如果你找不到Build.VERSION_CODES.LOLLIPOP,麻烦你将android的sdk开发版本先调到5.0或者5.0以上就ok了,注意pendingIntent1中的红色部门,这就是关键了,new 一个Intent,别的什么都不写,就ok了,问题解决,完事儿~!

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐