Sunday, July 10

Android Notification using NotificationCompat

NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle();
        style.bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.banner));
        style.setBigContentTitle("BigContentTitle");
        style.bigLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_large_banner)); // Icon set right side when notification size big
        style.setSummaryText("SummaryText");

        android.support.v4.app.NotificationCompat.Action replayAction = new NotificationCompat.Action.Builder(R.mipmap.ic_launcher, "Replay", PendingIntent.getActivities(this, 1, new Intent[]{new Intent(this, ReplayActivity.class)}, PendingIntent.FLAG_UPDATE_CURRENT))
                .addRemoteInput(new RemoteInput.Builder("KEY").setAllowFreeFormInput(true).setLabel("Enter text you want to send").build())
                .build();

        android.support.v4.app.NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_notification_icons) // Must Required
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_small_icon))
                .setContentTitle("Notifications Example")
                .setContentText("This is simple Notification Demo!!!")
                .setContentInfo("2")   // setContentInfo and setNumber both are same works when Subtext Not Available
                .setNumber(2)
                .setColor(Color.parseColor("#66bb6a"))
                .setOngoing(true)   // Unable to dismiss Notification
                .setShowWhen(true)  // Display Time in Notification
                .setPriority(Notification.PRIORITY_HIGH)
                .setDefaults(Notification.DEFAULT_VIBRATE) // For Heads-up Notification vibrate require API > 21 & setPriority Max or High
                .setTicker("Ticker Text") // Depricated in API 21
                .setSubText("Subtext of notification")
                .setStyle(style)
                .addAction(R.mipmap.ic_launcher, "Open", PendingIntent.getActivities(this, 1, new Intent[]{new Intent(this, MainActivity.class)}, PendingIntent.FLAG_UPDATE_CURRENT))
                .addAction(replayAction)
//              .setStyle(new NotificationCompat.BigTextStyle().bigText("Android powers hundreds of millions of mobile devices in more than 190 countries around the world. It's the largest installed base of any mobile platform and growing fast—every day another million users power up their Android devices for the first time and start looking for apps, games, and other digital content."))
                .setGroupSummary(true)  // 4 or More Notification you can group
                .setLights(Color.parseColor("#FF0000"), 5000, 5000) // Blink Light Of your Phone
                .setAutoCancel(true)
                .setProgress(100, 50, false)
                .setUsesChronometer(true) // set Chronometer on notification
                ;
        android.app.NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        mgr.notify(1, builder.build());
More detail on Android Notifications 
Thanks 

No comments:

Post a Comment