+ * TODO: Customize class - update intent actions and extra parameters. + */ +public class BadgeIntentService extends IntentService { + private int notificationId = 0; + + public BadgeIntentService() { + super("BadgeIntentService"); + } + + private NotificationManager mNotificationManager; + + public static void start(Context context, int badgeCount, String title, String message) { + Intent i = new Intent(context, BadgeIntentService.class); + i.putExtra("badgeCount", badgeCount); + i.putExtra("title", title); + i.putExtra("message", message); + context.startService(i); + } + + public static void start(Context context, int badgeCount, String title, String message, String uuid, long xstsid) { + Intent i = new Intent(context, BadgeIntentService.class); + i.putExtra("badgeCount", badgeCount); + i.putExtra("title", title); + i.putExtra("uuid", uuid); + i.putExtra("xstsid", xstsid); + i.putExtra("message", message); + context.startService(i); + } + + public BadgeIntentService(String name) { + super(name); + } + + @Override + public void onStart(Intent intent, int startId) { + super.onStart(intent, startId); + mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + } + + @Override + protected void onHandleIntent(Intent intent) { + if (intent != null) { + int badgeCount = intent.getIntExtra("badgeCount", 0); + String title = intent.getStringExtra("title"); + String message = intent.getStringExtra("message"); + //String uuid, long xstsid + String uuid = intent.getStringExtra("uuid"); + long xstsid = intent.getLongExtra("xstsid", -1); + if (xstsid == -1 || TextUtils.isEmpty(uuid)) + createIntentNotification(badgeCount, title, message); + if (xstsid != -1 && !TextUtils.isEmpty(uuid)) + createPendingIntentNotification(badgeCount, title, message, uuid, xstsid); + + } + } + + private void createIntentNotification(int badgeCount, String title, String message) { + if (mNotificationManager == null) { + mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + } + if (title == null) title = ""; + if (message == null) message = ""; + if (mNotificationManager != null) + mNotificationManager.cancel(notificationId); + + notificationId++; + + Notification.Builder builder = new Notification.Builder(getApplicationContext()) + .setContentTitle(title) + .setContentText(message) + .setSmallIcon(R.mipmap.ic_launcher); + Notification notification = Build.VERSION.SDK_INT > 15 ? builder.build() : builder.getNotification(); + ShortcutBadger.applyNotification(getApplicationContext(), notification, badgeCount); + if (mNotificationManager != null) + mNotificationManager.notify(notificationId, notification); + } + + private void createPendingIntentNotification(int badgeCount, String title, String message, String uuid, long xstsid) { + if (mNotificationManager == null) { + mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + } + Intent resultIntent = new Intent(AppContext.getInstance(), MainActivity.class); + resultIntent.putExtra("sid", xstsid); + resultIntent.putExtra("uuid", uuid); + mNotificationManager.cancel(notificationId); + notificationId = (int) xstsid; + //resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); + PendingIntent resultPendingIntent = + PendingIntent.getActivity( + AppContext.getInstance(), + 0, + resultIntent, + PendingIntent.FLAG_UPDATE_CURRENT + ); + NotificationCompat.Builder mBuilder = + new NotificationCompat.Builder(AppContext.getInstance()) + .setSmallIcon(R.mipmap.ic_launcher) + .setContentTitle("通知") + .setContentText(message) + //.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE) + .setAutoCancel(true) + .setContentIntent(resultPendingIntent); + AppConfig config = AppConfig.getAppConfig(AppContext.getInstance()); + boolean vibrate = Boolean.parseBoolean(config.get(AppConfig.NIM_CONFIG_VIBRATE));//XSTApp.instance.getVibrateState(); + boolean sound = Boolean.parseBoolean(config.get(AppConfig.NIM_CONFIG_SOUND));//XSTApp.instance.getRingState(); + if (vibrate && sound) { + mBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE); + } else if (vibrate) { + mBuilder.setDefaults(Notification.DEFAULT_VIBRATE); + } else if (sound) { + mBuilder.setDefaults(Notification.DEFAULT_SOUND); + } + Notification notification = mBuilder.build(); + ShortcutBadger.applyNotification(getApplicationContext(), notification, badgeCount); + mNotificationManager.notify((int) xstsid, notification); + + } +} -- libgit2 0.21.0