NCFirebaseMessagingService.java 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * Nextcloud Android client application
  3. *
  4. * @author Mario Danic
  5. * Copyright (C) 2017 Mario Danic
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.owncloud.android.services.firebase;
  21. import android.app.NotificationManager;
  22. import android.app.PendingIntent;
  23. import android.content.Context;
  24. import android.content.Intent;
  25. import android.graphics.BitmapFactory;
  26. import android.media.RingtoneManager;
  27. import android.net.Uri;
  28. import android.support.v4.app.NotificationCompat;
  29. import com.google.firebase.messaging.FirebaseMessagingService;
  30. import com.google.firebase.messaging.RemoteMessage;
  31. import com.owncloud.android.MainApp;
  32. import com.owncloud.android.R;
  33. import com.owncloud.android.ui.activity.NotificationsActivity;
  34. public class NCFirebaseMessagingService extends FirebaseMessagingService {
  35. private static final String TAG = "NCFirebaseMessaging";
  36. @Override
  37. public void onMessageReceived(RemoteMessage remoteMessage) {
  38. super.onMessageReceived(remoteMessage);
  39. sendNotification(MainApp.getAppContext().getString(R.string.new_notification_received));
  40. }
  41. private void sendNotification(String contentTitle) {
  42. Intent intent = new Intent(this, NotificationsActivity.class);
  43. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  44. PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
  45. PendingIntent.FLAG_ONE_SHOT);
  46. NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
  47. .setSmallIcon(R.drawable.notification_icon)
  48. .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.notification_icon))
  49. .setContentTitle(contentTitle)
  50. .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
  51. .setAutoCancel(true)
  52. .setContentIntent(pendingIntent);
  53. NotificationManager notificationManager =
  54. (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  55. notificationManager.notify(0, notificationBuilder.build());
  56. }
  57. }