Aurora push problem summary

Aurora push

The company's project integrates Jiguang push, recently fixed some previous bugs, and made a summary. Don't say much nonsense, let me explain it~~


1. Unable to receive the push or the previous login device received the push

Definition of RegistrationID When an application
integrated with the JPush SDK is successfully registered to the JPush server for the first time, the JPush server will return a unique identification of the device to the client - RegistrationID
The following code is part of the code in the custom broadcast receiver, using to receive RegistrationID

 if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            sp.edit().putString("jpush_regis_id", regId).apply();
            DLog.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
            // send the Registration Id to your server。。。

        } 

The logic in our background is to push messages or notifications based on the RegistrationID we passed when logging in. If it is saved as above, then the problem arises. The broadcast Action-JPushInterface that receives the push message. ACTION_REGISTRATION_ID will only be called back once, then when you switch accounts, or when you run the project during development, instantiating Jiguang Push will not receive the broadcast, resulting in the RegistrationID in the shared file being empty, log in The one passed to the background is also empty, and the background will push it to the device that logged in last time and recorded the RegistrationID instead of the currently logged in device.

Solution: getRegistrationID(Context context) Call this method to get the current RegistrationID and pass it to the background.

2. Push notification icons and styles are not uniform

We currently push notifications directly to us in the background. On some mobile phones, the default push notification style cannot be displayed well on our status bar and drop-down notification bar, and some mobile phones may have problems such as incomplete display of icons on the status bar. . .

Solution: Use the method of customizing the notification bar. Note that the size of the small icon in the topmost status bar needs to be adapted. The size of the notification icon is drawable-xhdpi 48*48 drawable-hdpi 36*36 drawable-mdpi 24*24. When pulling down the status bar The displayed notification icon can directly use the application icon. The code is as follows:

  CustomPushNotificationBuilder builder = new
                CustomPushNotificationBuilder(YMApplication.getInstance(),
                R.layout.customer_notitfication_layout,
                R.id.icon,
                R.id.title,
                R.id.text);
        // 指定定制的 Notification Layout
        builder.statusBarDrawable = R.drawable.jpush_notification_icon;
        // 指定最顶层状态栏小图标
        builder.layoutIconDrawable = R.mipmap.dp_icon;
        // 指定下拉状态栏时显示的通知图标
        JPushInterface.setPushNotificationBuilder(1, builder);

3. Closing and opening of front-end and back-end push

In response to demand, we need the application to receive notifications when it is in the background, but not when it is in the foreground.

Solution: Set the aurora push by monitoring whether the application switches to the foreground or the background, see the code for details

    private int mStartedActivityCount = 0; //活动的Activity个数
    private Set<Integer> mDaySetClear = new HashSet<>();
    private Set<Integer> mDaySetAll = new HashSet<>();
        mDaySetAll.add(0);
        mDaySetAll.add(1);
        mDaySetAll.add(2);
        mDaySetAll.add(3);
        mDaySetAll.add(4);
        mDaySetAll.add(5);
        mDaySetAll.add(6);
//监听前后台
 registerActivityLifecycleCallbacks(
                    new Application.ActivityLifecycleCallbacks() {
                        @Override
                        public void onActivityCreated(
                                Activity activity, Bundle bundle) {

                        }

                        @Override
                        public void onActivityStarted(Activity activity) {
                            mStartedActivityCount++;
                            if (!mAppIsForeGround) {
                                mAppIsForeGround = true;
                             JPushInterface.setPushTime(YMApplication.getInstance(), mDaySetClear, 0, 23);
                             //设置推送时间来控制推送能否收到
                            }
                        }

                        @Override
                        public void onActivityResumed(Activity activity) {

                        }

                        @Override
                        public void onActivityPaused(Activity activity) {

                        }

                        @Override
                        public void onActivityStopped(Activity activity) {
                            mStartedActivityCount--;
                            if (mStartedActivityCount == 0) {
                                if (mAppIsForeGround) {
                                    mAppIsForeGround = false;
                                    JPushInterface.setPushTime(YMApplication.getInstance(), mDaySetAll, 0, 23);
                                }
                            }
                        }

                        @Override
                        public void onActivitySaveInstanceState(
                                Activity activity, Bundle bundle) {

                        }

                        @Override
                        public void onActivityDestroyed(Activity activity) {

                        }
                    });

4. Notification bar style adaptation

There are two types of notifications, default and custom.

The default notification does not have the problem of style adaptation, because the layout, color, and background of the default notification are all system, and the system will always display the default notification correctly.
But the custom notification is different. The layout of the custom notification is completely controlled by us, and we can set any background and color for the element. Well, here comes the question. There are various backgrounds of the Android notification bar, different ROMs have different backgrounds, white, black, transparent, etc. Different Android versions have different notification bar backgrounds. Once we set specific backgrounds or colors for elements on custom notifications, it will definitely bring compatibility issues (mainly text).

Solution: 1. Directly specify a background color and font color. This is simple and easy to implement, but it is not immersive enough with the system notification bar, which affects the overall appearance. 2. Another solution is slightly more reasonable: by reading the notification bar style file of the system, get the color of the title and content, and then set the color to the custom notification. There is a compatibility problem in reading the notification bar style file itself, and the style files of different Android versions change. For details, please refer to this blog Notification bar setting system font color , this method does not take effect on all mobile phones, the actual test found that it is still There are a small number of models that cannot be read or what is read is wrong. After getting the color of the title and content, you can also judge whether the color is approximately white or approximately black through an algorithm (detailed later), and then you can determine whether the background of the notification bar is approximately black or approximately white, so that it can be determined according to different notifications. Bar background loads different custom notification layouts. to achieve a good fit.
The notification bar has various background colors, font sizes and colors. Through the above method, some models cannot get the color of the system notification bar, but through observation, it can be found that all models that cannot get the font color are dark or black background (the actual experience of 7.0 is invalid), so you can use White font.

Reference link:
https://www.jianshu.com/p/426d85f34561

http://iluhcm.com/2017/03/12/experience-of-adapting-to-android-notifications/#%E5%B0%8F%E5%9B%BE%E6%A0%87%E6%98%BE%E7%A4%BA%E5%BC%82%E5%B8%B8

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325525564&siteId=291194637