Skip to content

Commit

Permalink
FCM Added
Browse files Browse the repository at this point in the history
  • Loading branch information
whoishusni committed Apr 23, 2020
1 parent 5078338 commit ebc6b02
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 3 deletions.
5 changes: 4 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.google.gms.google-services'

android {
compileSdkVersion 29
Expand All @@ -12,7 +13,8 @@ android {
targetSdkVersion 29
versionCode 2
versionName "1.3"
buildConfigField("String","KEY_NEWS_API",'"{INSERT YOUT API FROM NEWSAPI_HERE}"') // TODO : INSERT YOUT API FROM NEWSAPI_HERE
//TODO : INSERT YOUR API FROM NEWSAPI.ORG HERE
buildConfigField("String", "KEY_NEWS_API", '"{API_KEY_HERE}"')
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

Expand All @@ -35,6 +37,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-messaging:20.1.5'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
Expand Down
40 changes: 40 additions & 0 deletions app/google-services.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"project_info": {
"project_number": "1073906460226",
"firebase_url": "https://sulteng-covid-8d14c.firebaseio.com",
"project_id": "sulteng-covid-8d14c",
"storage_bucket": "sulteng-covid-8d14c.appspot.com"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:1073906460226:android:2bb8958993b4d4acc46fdb",
"android_client_info": {
"package_name": "id.husni.sultengcovid"
}
},
"oauth_client": [
{
"client_id": "1073906460226-a2k37h2l8v4q7aki0el2mgqar7n5aj47.apps.googleusercontent.com",
"client_type": 3
}
],
"api_key": [
{
"current_key": "AIzaSyCZlSFa_7NotKhedN7zwHagfzxfDOklkRw"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": [
{
"client_id": "1073906460226-a2k37h2l8v4q7aki0el2mgqar7n5aj47.apps.googleusercontent.com",
"client_type": 3
}
]
}
}
}
],
"configuration_version": "1"
}
13 changes: 12 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package="id.husni.sultengcovid">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.VIBRATE"/>

<application
android:allowBackup="true"
Expand All @@ -11,7 +13,16 @@
android:roundIcon="@drawable/icon"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".activity.NewsActivity"></activity>
<service
android:name=".serviceapi.FcmCloudMessageService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>

<activity android:name=".activity.NewsActivity" />
<activity android:name=".activity.PreventionActivity" />
<activity android:name=".activity.OpeningActivity">
<intent-filter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import android.os.Bundle
import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.facebook.shimmer.BuildConfig
import id.husni.sultengcovid.BuildConfig
import id.husni.sultengcovid.R
import kotlinx.android.synthetic.main.activity_about.*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ package id.husni.sultengcovid.activity
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProvider.NewInstanceFactory
import com.google.firebase.iid.FirebaseInstanceId
import com.google.firebase.messaging.FirebaseMessaging
import id.husni.sultengcovid.R
import id.husni.sultengcovid.model.Province
import id.husni.sultengcovid.viewmodel.ProvinceViewModel
Expand All @@ -31,6 +34,11 @@ class MainActivity : AppCompatActivity() {
supportActionBar?.title = getString(R.string.app_name)
setSupportActionBar(mainToolbar)

FirebaseMessaging.getInstance().subscribeToTopic("all")
FirebaseInstanceId.getInstance().instanceId.addOnSuccessListener {
Log.d("Token", it.token)
}

mainViewPager.adapter = MainPagerAdapter(this,supportFragmentManager)
mainTabLayout.setupWithViewPager(mainViewPager)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (c) 2020.
* Made with ❤ by Moh Husni Mubaraq
* Not For Commercial Purpose
*/

package id.husni.sultengcovid.serviceapi

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.media.RingtoneManager
import android.net.Uri
import android.os.Build
import android.util.Log
import androidx.core.app.NotificationCompat
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import id.husni.sultengcovid.R

class FcmCloudMessageService : FirebaseMessagingService() {
private var notifId = 1
companion object{
const val CHANNEL_ID = "channelOne"
const val CHANNEL_NAME = "channelFcm"
const val FCM_REQUEST_CODE = 100
const val limitNotif = 2
}

override fun onNewToken(p0: String) {
super.onNewToken(p0)
Log.d("NEW TOKEN", p0)
}

override fun onMessageReceived(remoteMessage: RemoteMessage) {
super.onMessageReceived(remoteMessage)
remoteMessage.notification?.let {
getNotification(it.body)
notifId++
}
}

private fun getNotification(body: String?) {
val intent = Intent()
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
val pendingIntent : PendingIntent = PendingIntent.getActivity(this,FCM_REQUEST_CODE, intent, PendingIntent.FLAG_ONE_SHOT)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val uriSound : Uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder : NotificationCompat.Builder

if (notifId < limitNotif){
val largeIcon = BitmapFactory.decodeResource(resources,R.drawable.icon)
notificationBuilder = NotificationCompat.Builder(this, CHANNEL_ID)
.setContentText(body)
.setSmallIcon(R.drawable.icon)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setVibrate(longArrayOf(1000,1000,1000,1000,1000))
.setSound(uriSound)
.setLargeIcon(largeIcon)
}
else{
val inboxStyle = NotificationCompat.InboxStyle()
.setBigContentTitle("s")
.setSummaryText(notifId.toString() + getString(R.string.message))
.addLine(body)
.addLine(body)

notificationBuilder = NotificationCompat.Builder(this, CHANNEL_ID)
.setContentText(body)
.setSmallIcon(R.drawable.icon)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setVibrate(longArrayOf(1000,1000,1000,1000,1000))
.setSound(uriSound)
.setStyle(inboxStyle)
}

if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.O){
val channel = NotificationChannel(CHANNEL_ID,CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT)
notificationManager.createNotificationChannel(channel)
notificationBuilder.setChannelId(CHANNEL_ID)
}
val notification = notificationBuilder.build()
notificationManager.notify(notifId,notification)
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@
<string name="_5_better_at_home">5. Better at Home</string>
<string name="news_api">NewsApi</string>
<string name="news">News</string>
<string name="message">\" Message\"</string>
</resources>
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:3.6.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.3.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down

0 comments on commit ebc6b02

Please sign in to comment.