Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyNotificationManager {
- public static final int ID_BIG_NOTIFICATION = 234;
- public static final int ID_SMALL_NOTIFICATION = 235;
- private Context mCtx;
- public MyNotificationManager(Context mCtx) {
- this.mCtx = mCtx;
- }
- //the method will show a big notification with an image
- //parameters are title for message title, message for message text, url of the big image and an intent that will open
- //when you will tap on the notification
- public void showBigNotification(String title, String message, Bitmap image, Intent intent) {
- int requestID = (int) System.currentTimeMillis();
- PendingIntent resultPendingIntent =
- PendingIntent.getActivity(
- mCtx,
- requestID,
- intent,
- 0
- );
- Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
- bigPictureStyle.bigPicture(image);
- bigPictureStyle.setSummaryText(message);
- Notification.Builder mBuilder = new Notification.Builder(mCtx);
- Notification notification;
- notification = mBuilder.setSmallIcon(R.drawable.notification_icon).setTicker(title).setWhen(0)
- .setAutoCancel(true)
- .setContentTitle(title)
- .setStyle(bigPictureStyle)
- .setSmallIcon(R.drawable.notification_icon)
- .setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(),R.drawable.mr_hot_circle))
- .addAction(R.drawable.logout_icon,"Open App",resultPendingIntent)
- //.setContentIntent(resultPendingIntent)
- .build();
- notification.flags |= Notification.FLAG_AUTO_CANCEL;
- NotificationManager notificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
- notificationManager.notify(requestID, notification);
- }
- //the method will show a small notification
- //parameters are title for message title, message for message text and an intent that will open
- //when you will tap on the notification
- public void showSmallNotification(String title, String message, Intent intent) {
- int requestID = (int) System.currentTimeMillis();
- PendingIntent resultPendingIntent =
- PendingIntent.getActivity(
- mCtx,
- requestID,
- intent,
- 0
- );
- Notification.Builder mBuilder = new Notification.Builder(mCtx);
- Notification notification;
- notification = mBuilder.setSmallIcon(R.drawable.notification_icon).setTicker(title).setWhen(0)
- .setAutoCancel(true)
- .setContentIntent(resultPendingIntent)
- .setContentTitle(title)
- .setSmallIcon(R.drawable.notification_icon)
- .setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(),R.drawable.mr_hot_circle))
- .setContentText(message)
- .build();
- notification.flags |= Notification.FLAG_AUTO_CANCEL;
- NotificationManager notificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
- notificationManager.notify(requestID, notification);
- }
- //The method will return Bitmap from an image URL
- public class MyFirebaseMessagingService extends FirebaseMessagingService {
- private static final String TAG = "MyFirebaseMsgService";
- MyNotificationManager mNotificationManager;
- String title,message,imageUrl;
- Intent intent;
- @Override
- public void onMessageReceived(RemoteMessage remoteMessage) {
- if (remoteMessage.getData().size() > 0) {
- Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
- try {
- if (remoteMessage.getNotification() != null) {
- Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
- }
- title = remoteMessage.getData().get("title");
- message = remoteMessage.getData().get("message");
- imageUrl = remoteMessage.getData().get("image");
- //creating MyNotificationManager object
- mNotificationManager = new MyNotificationManager(getApplicationContext());
- //creating an intent for the notification
- intent = new Intent(getApplicationContext(), HomeActivity.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
- /*TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
- stackBuilder.addParentStack(Welcome.class);
- stackBuilder.addNextIntent(intent);*/
- //if there is no image
- if(imageUrl.equals("")){
- //displaying small notification
- mNotificationManager.showSmallNotification(title, message, intent);
- }else{
- //if there is an image
- //displaying a big notification
- getBitmapFromURL(imageUrl);
- }
- } catch (Exception e) {
- Log.e(TAG, "Exception: " + e.getMessage());
- }
- }
- }
- private void getBitmapFromURL(String strURL) {
- try {
- URL url = new URL(strURL);
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.setDoInput(true);
- connection.connect();
- InputStream input = connection.getInputStream();
- Bitmap myBitmap = BitmapFactory.decodeStream(input);
- mNotificationManager.showBigNotification(title, message, myBitmap, intent);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement