This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<androidx.appcompat.widget.AppCompatButton | |
android:id="@+id/btn_display_basic_notification" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.pushnotificaiton.utils.push | |
/** | |
* Created by KING JINHO on 2020-06-23 | |
*/ | |
class NotificationBuilder { | |
companion object { | |
private lateinit var CHANNEL_ID: String |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
... | |
btn_display_basic_notification.setOnClickListener { | |
NotificationBuilder.from(this) | |
.createNotificationBuilder("HelloWorld") | |
.setTitle(R.string.app_name) | |
.setContent(R.string.app_name) | |
.setSmallIcon(R.drawable.ic_baseline_add_circle_24) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Builder { | |
... | |
... | |
CharSequence mContentTitle; | |
CharSequence mContentText; | |
PendingIntent mContentIntent; | |
PendingIntent mFullScreenIntent; | |
RemoteViews mTickerView; | |
Bitmap mLargeIcon; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun setSmallIcon(smallIconRes: Int): NotificationBuilder { | |
mNotificationBuilder.setSmallIcon(smallIconRes) | |
return this | |
} | |
fun setTitle(titleRes: Int): NotificationBuilder { | |
mNotificationBuilder.setContentTitle(CONTEXT.getString(titleRes)) | |
return this | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun notifyUser(num : Int) { | |
mNotificationManagerCompat = NotificationManagerCompat.from(CONTEXT) | |
//id: unique in your app. otherwise, it will update to new one | |
mNotificationManagerCompat.notify(num, mNotificationBuilder.build()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Compatibility library for NotificationManager with fallbacks for older platforms. | |
* | |
* <p>To use this class, call the static function {@link #from} to get a | |
* {@link NotificationManagerCompat} object, and then call one of its | |
* methods to post or cancel notifications. | |
*/ | |
public final class NotificationManagerCompat { | |
private static final String TAG = "NotifManCompat"; | |
private static final String CHECK_OP_NO_THROW = "checkOpNoThrow"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private NotificationManagerCompat(Context context) { | |
mContext = context; | |
mNotificationManager = (NotificationManager) mContext.getSystemService( | |
Context.NOTIFICATION_SERVICE); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun setLargeIcon(largeIconRes: Int): NotificationBuilder { | |
val bitmap = BitmapFactory.decodeResource(CONTEXT.resources, largeIconRes) | |
mNotificationBuilder.setLargeIcon(bitmap) | |
return this | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun setBlockOfText(blockOfText: String): NotificationBuilder { | |
mNotificationBuilder.setStyle( | |
NotificationCompat.BigTextStyle() | |
//.setBigContentTitle("this is big content title") overrides setContentTitle() | |
//.setSummaryText("this is summary text") right next to app name | |
.bigText(blockOfText) | |
) | |
return this | |
} |
OlderNewer