仿微信通知欄

轉載請註明出處:blog.csdn.net/Chay_Chan/a…android

效果圖

點擊通知欄後跳轉並傳值
點擊通知欄後跳轉並傳值

app下載體驗

點擊下載git

通知欄圖片

通知欄的小圖標建議使用純白色,在通知欄顯示的時候就是白色,拉下來的時候就會變成灰色的
在圖片文件夾中放置不一樣尺寸的圖標github

24 × 24 (mdpi)
36 × 36 (hdpi)
48 × 48 (xhdpi)
72 × 72 (xxhdpi)
96 × 96 (xxxhdpi)緩存

封裝好的通知工具類

調用彈出通知欄的方法:bash

public static void showNotification(Context context, String conversationType, String targetId, String avatar, String name, String content) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);

        mBuilder.setPriority(Notification.PRIORITY_MAX);//可讓通知顯示在最上面
        mBuilder.setSmallIcon(R.mipmap.small_icon);
        mBuilder.setWhen(System.currentTimeMillis());
        mBuilder.setAutoCancel(true);

        if (shouldRemind(true)) {
            mBuilder.setDefaults(Notification.DEFAULT_ALL);//使用默認的聲音、振動、閃光
        }

        Bitmap bmIcon = BitmapFactory.decodeResource(
                context.getResources(), R.mipmap.ic_launcher);

        Bitmap largeIcon = bmIcon;
        Bitmap bmAvatar = GetImageInputStream(avatar);
        if (bmAvatar != null) {
            //若是能夠獲取到網絡頭像則用網絡頭像
            largeIcon = bmAvatar;
        }

        content = name + ":" + content; //內容爲 xxx:內容
        //        int unreadCount = RongIMClient.getInstance().getUnreadCount(conversationType, targetId);
        //        if (unreadCount > 1) {
        //            //若是未讀數大於1,則還有拼接上[x條]
        //            String num = String.format(UIUtils.getString(R.string.notification_num_format), unreadCount);
        //            content = num + content;//內容爲 [x條] xxx:內容
        //        }

        mBuilder.setLargeIcon(largeIcon);

        mBuilder.setContentTitle(name);

        Intent intent = getIntent(context, conversationType, targetId, name);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, getRandomNum(), intent, PendingIntent.FLAG_UPDATE_CURRENT);

        //通知首次出如今通知欄,帶上升動畫效果的
        mBuilder.setTicker(content);
        //內容
        mBuilder.setContentText(content);

        mBuilder.setContentIntent(pendingIntent);
        Notification notification = mBuilder.build();

        int notifyId = 0;
        try {
            notifyId = Integer.parseInt(targetId);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            notifyId = -1;
        }

        //彈出通知欄
        notificationManager.notify(notifyId, notification);
    }複製代碼

獲取網絡圖片(頭像)的方法網絡

/**
     * 獲取網絡圖片
     *
     * @param imageurl 圖片網絡地址
     * @return Bitmap 返回位圖
     */
    public static Bitmap GetImageInputStream(String imageurl) {
        URL url;
        HttpURLConnection connection = null;
        Bitmap bitmap = null;
        try {
            url = new URL(imageurl);
            connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(6000); //超時設置
            connection.setDoInput(true);
            connection.setUseCaches(false); //設置不使用緩存
            InputStream inputStream = connection.getInputStream();
            bitmap = BitmapFactory.decodeStream(inputStream);
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }複製代碼

通知欄跳轉意圖的建立app

/**
     * 產生對應的intent
     */
    private static Intent getIntent(Context context, String conversationType, String targetId, String title) {
        Intent intent = new Intent();
        intent.setAction("ActionConversation");
        intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK);

        Uri.Builder uriBuilder = Uri.parse("wechat://" + context.getPackageName()).buildUpon();

        uriBuilder.appendPath("conversation");
        uriBuilder.appendQueryParameter(Constant.CONVERSATION_TYPE, conversationType)
                .appendQueryParameter(Constant.TARGET_ID, targetId)
                .appendQueryParameter(Constant.TITLE, title);

        intent.setData(uriBuilder.build());
        Log.i("uri: ", uriBuilder.build().toString());
        return intent;
    }複製代碼

在Manifest中,配置對應配置點擊通知欄跳轉的相應activity的uri配置:dom

<activity
            android:name=".MainActivity"
            android:configChanges="keyboardHidden|screenSize|orientation"
            android:screenOrientation="portrait"
            android:launchMode="singleTask"
            >

            ...

            <intent-filter>
                <action android:name="ActionConversation"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data
                    android:host="com.chaychan.wechatnotification"
                    android:pathPrefix="/conversation"
                    android:scheme="wechat"/>
            </intent-filter>

  </activity>複製代碼

其中 intent-filter 中配置了activity的匹配規則,action對應getIntent()方法中設置的action的值,data中的scheme和pathPrefit也對應getIntent()方法中設置的uri的路徑。ide

MainActivity中處理傳遞的Intent

@Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Uri uri = intent.getData();
        if (!uri.getScheme().equals("wechat")) {
            return;
        }
        if (uri.getPath().contains("/conversation")) {
            String conversationType = uri.getQueryParameter(Constant.CONVERSATION_TYPE);
            String targetId = uri.getQueryParameter(Constant.TARGET_ID);
            String title = uri.getQueryParameter(Constant.TITLE);

            Intent conversationIntent = new Intent(this, ConversationActivity.class);
            conversationIntent.putExtra(Constant.TITLE,title);
            conversationIntent.putExtra(Constant.TARGET_ID,targetId);
            conversationIntent.putExtra(Constant.CONVERSATION_TYPE,conversationType);
            startActivity(conversationIntent);
        }
    }複製代碼

在onNewIntent()處理通知欄傳遞過來的intent,根據傳遞過來的uri進行跳轉。 工具

源碼下載:

github.com/chaychan/We…

相關文章
相關標籤/搜索