NCFirebaseMessagingService.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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.media.RingtoneManager;
  26. import android.net.Uri;
  27. import android.support.v4.app.NotificationCompat;
  28. import com.google.firebase.messaging.FirebaseMessagingService;
  29. import com.google.firebase.messaging.RemoteMessage;
  30. import com.owncloud.android.MainApp;
  31. import com.owncloud.android.R;
  32. import com.owncloud.android.ui.activity.NotificationsActivity;
  33. public class NCFirebaseMessagingService extends FirebaseMessagingService {
  34. private static final String TAG = "NCFirebaseMessaging";
  35. @Override
  36. public void onMessageReceived(RemoteMessage remoteMessage) {
  37. super.onMessageReceived(remoteMessage);
  38. sendNotification(MainApp.getAppContext().getString(R.string.new_notification_received));
  39. }
  40. private void sendNotification(String contentTitle) {
  41. Intent intent = new Intent(this, NotificationsActivity.class);
  42. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  43. PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
  44. PendingIntent.FLAG_ONE_SHOT);
  45. Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  46. NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
  47. .setSmallIcon(R.mipmap.ic_launcher)
  48. .setContentTitle(contentTitle)
  49. .setSound(defaultSoundUri)
  50. .setAutoCancel(true)
  51. .setContentIntent(pendingIntent);
  52. NotificationManager notificationManager =
  53. (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  54. notificationManager.notify(0, notificationBuilder.build());
  55. }
  56. }