Skip to content

Commit bf08369

Browse files
author
Aristos Pasalides
committed
Fixed crash in API31, not having declared mutability on PendingIntent.
1 parent edf44e4 commit bf08369

File tree

1 file changed

+17
-5
lines changed
  • notify/src/main/java/com/application/isradeleon/notify

1 file changed

+17
-5
lines changed

notify/src/main/java/com/application/isradeleon/notify/Notify.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package com.application.isradeleon.notify;
22

3+
import static android.app.PendingIntent.FLAG_CANCEL_CURRENT;
4+
import static android.app.PendingIntent.FLAG_IMMUTABLE;
5+
import static android.os.Build.VERSION.SDK_INT;
6+
import static android.os.Build.VERSION_CODES.M;
7+
8+
import android.annotation.SuppressLint;
39
import android.app.Notification;
410
import android.app.NotificationChannel;
511
import android.app.NotificationManager;
@@ -90,6 +96,7 @@ private Notify(Context _context){
9096

9197
public static Notify build(@NonNull Context context){ return new Notify(context); }
9298

99+
@SuppressLint("UnspecifiedImmutableFlag")
93100
public void show(){
94101
if (context == null) return;
95102

@@ -139,14 +146,14 @@ public void show(){
139146
* Set notification color
140147
* */
141148
int realColor;
142-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) realColor = color == -1 ? Color.BLACK : context.getResources().getColor(color, null);
149+
if (SDK_INT >= M) realColor = color == -1 ? Color.BLACK : context.getResources().getColor(color, null);
143150
else realColor = color == -1 ? Color.BLACK : context.getResources().getColor(color);
144151
builder.setColor(realColor);
145152

146153
/*
147154
* Oreo^ notification channels
148155
* */
149-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
156+
if (SDK_INT >= Build.VERSION_CODES.O) {
150157
NotificationChannel notificationChannel = new NotificationChannel(
151158
channelId, channelName, oreoImportance
152159
);
@@ -171,7 +178,12 @@ public void show(){
171178
* Action triggered when user clicks noti
172179
* */
173180
if(this.action != null){
174-
PendingIntent pi = PendingIntent.getActivity(context, id, this.action, PendingIntent.FLAG_CANCEL_CURRENT);
181+
PendingIntent pi;
182+
if (SDK_INT >= M)
183+
pi = PendingIntent.getActivity(context, id, this.action,
184+
FLAG_CANCEL_CURRENT | FLAG_IMMUTABLE);
185+
else pi = PendingIntent.getActivity(context, id,
186+
this.action, FLAG_CANCEL_CURRENT);
175187
builder.setContentIntent(pi);
176188
}
177189

@@ -214,7 +226,7 @@ public Notify setChannelDescription(@NonNull String channelDescription) {
214226
}
215227

216228
public Notify setImportance(@NonNull NotifyImportance importance){
217-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
229+
if (SDK_INT >= Build.VERSION_CODES.N) {
218230
switch (importance){
219231
case MIN:
220232
this.importance = Notification.PRIORITY_LOW;
@@ -242,7 +254,7 @@ public Notify setImportance(@NonNull NotifyImportance importance){
242254

243255
private void setDefaultPriority(){
244256
this.importance = Notification.PRIORITY_DEFAULT;
245-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
257+
if (SDK_INT >= Build.VERSION_CODES.N)
246258
this.oreoImportance = NotificationManager.IMPORTANCE_DEFAULT;
247259
}
248260

0 commit comments

Comments
 (0)