Push notifications service for Android 4.4

后端 未结 2 1130
遇见更好的自我
遇见更好的自我 2021-01-15 06:07

I have done work on push notifications and its working correct but in case till api level 4.0. But notification click does not open the activity in case of api 4.4....I am n

2条回答
  •  半阙折子戏
    2021-01-15 06:25

    Try something like below:

            private final static int NOTIFICATION_ID = 1;
            private static NotificationManager mNotificationManager;
    
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
                // API 16 onwards
                Notification.Builder builder = new Notification.Builder(context);
                builder.setAutoCancel(false)
                    .setContentIntent(pendingIntent)
                    .setContentText(context.getString(R.string.notification_text))
                    .setContentTitle(context.getString(R.string.app_name))
                    .setOngoing(true)
                    .setSmallIcon(R.drawable.ic_notifier)
                    .setWhen(System.currentTimeMillis());
                Notification notification = builder.build();
                notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
                mNotificationManager.notify(NOTIFICATION_ID, notification);         
            } else {
                // API 15 and earlier
                NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
                builder.setAutoCancel(false)
                    .setContentIntent(pendingIntent)
                    .setContentText(context.getString(R.string.notification_text))
                    .setContentTitle(context.getString(R.string.app_name))
                    .setOngoing(true)
                    .setSmallIcon(R.drawable.ic_notifier)
                    .setWhen(System.currentTimeMillis());
                Notification notification = builder.getNotification();
                notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
                mNotificationManager.notify(NOTIFICATION_ID, notification);         
            }
    

    Add the JSON part where appropriate. And don't forget to add the android-support-v4.jar to your project or it won't compile.

提交回复
热议问题