From c3a8086edaa8d4cbd5f586828ba2709444aa3553 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Mon, 14 May 2018 11:11:17 +0300 Subject: [PATCH] Notifications init --- app/build.gradle | 9 +-- app/src/main/AndroidManifest.xml | 7 ++ .../mthmmy/activities/BookmarkActivity.java | 10 ++- .../mthmmy/services/FirebaseService.java | 72 +++++++++++++++++++ .../main/res/drawable/ic_notification_off.xml | 9 +++ .../main/res/drawable/ic_notification_on.xml | 5 ++ ...ow.xml => activity_bookmark_board_row.xml} | 4 +- .../layout/activity_bookmark_topic_row.xml | 42 +++++++++++ app/src/main/res/values/strings.xml | 1 + build.gradle | 4 +- 10 files changed, 153 insertions(+), 10 deletions(-) create mode 100644 app/src/main/java/gr/thmmy/mthmmy/services/FirebaseService.java create mode 100644 app/src/main/res/drawable/ic_notification_off.xml create mode 100644 app/src/main/res/drawable/ic_notification_on.xml rename app/src/main/res/layout/{activity_bookmark_row.xml => activity_bookmark_board_row.xml} (87%) create mode 100644 app/src/main/res/layout/activity_bookmark_topic_row.xml diff --git a/app/build.gradle b/app/build.gradle index 7faa0dd0..83430d97 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,6 +1,6 @@ apply plugin: 'com.android.application' -if (buildInRelease()){ +if (buildInRelease()) { apply plugin: 'io.fabric' } @@ -37,8 +37,9 @@ dependencies { implementation 'com.android.support:support-v4:27.1.1' implementation 'com.android.support:cardview-v7:27.1.1' implementation 'com.android.support:recyclerview-v7:27.1.1' - implementation 'com.google.firebase:firebase-core:15.0.0' - implementation 'com.crashlytics.sdk.android:crashlytics:2.9.1' + implementation 'com.google.firebase:firebase-core:15.0.2' + implementation 'com.google.firebase:firebase-messaging:15.0.2' + implementation 'com.crashlytics.sdk.android:crashlytics:2.9.2' implementation 'com.squareup.okhttp3:okhttp:3.10.0' implementation 'com.squareup.picasso:picasso:2.5.2' implementation 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0' @@ -56,7 +57,7 @@ dependencies { implementation 'com.jakewharton.timber:timber:4.7.0' } -if (buildInRelease()){ +if (buildInRelease()) { apply plugin: 'com.google.gms.google-services' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index bf5256cd..49b5d305 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -119,6 +119,13 @@ + + + + + 0) { + + Timber.i("msg%s", remoteMessage.getData()); + sendNotification("stuff"); + + } + + + } + + /** + * Create and show a simple notification containing the received FCM message. + */ + private void sendNotification(String message) { + Intent intent = new Intent(this, TopicActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, + PendingIntent.FLAG_ONE_SHOT); + + String channelId = "ASdf"; //getString(R.string.default_notification_channel_id); + Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); + NotificationCompat.Builder notificationBuilder = + new NotificationCompat.Builder(this, channelId) + .setSmallIcon(R.mipmap.ic_launcher) + .setContentTitle("FCM Message") + .setContentText(message) + .setAutoCancel(true) + .setSound(defaultSoundUri) + .setContentIntent(pendingIntent); + + NotificationManager notificationManager = + (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + + // Since android Oreo notification channel is needed. + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel channel = new NotificationChannel("Posts", + "New Posts", + NotificationManager.IMPORTANCE_DEFAULT); + + notificationManager.createNotificationChannel(channel); + + } + + + notificationManager.notify(0, notificationBuilder.build()); + } +} diff --git a/app/src/main/res/drawable/ic_notification_off.xml b/app/src/main/res/drawable/ic_notification_off.xml new file mode 100644 index 00000000..7588f1e2 --- /dev/null +++ b/app/src/main/res/drawable/ic_notification_off.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_notification_on.xml b/app/src/main/res/drawable/ic_notification_on.xml new file mode 100644 index 00000000..948bbac1 --- /dev/null +++ b/app/src/main/res/drawable/ic_notification_on.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/layout/activity_bookmark_row.xml b/app/src/main/res/layout/activity_bookmark_board_row.xml similarity index 87% rename from app/src/main/res/layout/activity_bookmark_row.xml rename to app/src/main/res/layout/activity_bookmark_board_row.xml index a5245a79..7d2632a2 100644 --- a/app/src/main/res/layout/activity_bookmark_row.xml +++ b/app/src/main/res/layout/activity_bookmark_board_row.xml @@ -4,7 +4,7 @@ android:id="@+id/bookmark_row" android:layout_width="match_parent" android:layout_height="wrap_content" - android:background="?attr/selectableItemBackground" + android:background="?android:attr/selectableItemBackground" android:clickable="true" android:focusable="true" android:gravity="center_vertical" @@ -27,7 +27,7 @@ android:id="@+id/remove_bookmark" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:background="?attr/selectableItemBackground" + android:background="?android:attr/selectableItemBackground" android:contentDescription="@string/remove_bookmark" android:src="@drawable/ic_remove_circle"/> \ No newline at end of file diff --git a/app/src/main/res/layout/activity_bookmark_topic_row.xml b/app/src/main/res/layout/activity_bookmark_topic_row.xml new file mode 100644 index 00000000..240268e6 --- /dev/null +++ b/app/src/main/res/layout/activity_bookmark_topic_row.xml @@ -0,0 +1,42 @@ + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 73a8c8e4..1d63348c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -90,4 +90,5 @@ + Toggle Notification diff --git a/build.gradle b/build.gradle index c9e5f745..731a4e70 100644 --- a/build.gradle +++ b/build.gradle @@ -9,8 +9,8 @@ buildscript { google() } dependencies { - classpath 'com.android.tools.build:gradle:3.1.1' - classpath 'com.google.gms:google-services:3.2.0' + classpath 'com.android.tools.build:gradle:3.1.2' + classpath 'com.google.gms:google-services:3.3.0' classpath 'io.fabric.tools:gradle:1.25.1' } }