Skip to content

Commit

Permalink
Merge pull request #40 from defold/context_for_notifications
Browse files Browse the repository at this point in the history
Fix NullPointerException
  • Loading branch information
AGulev committed Nov 3, 2021
2 parents b148229 + bad51b5 commit a8a539d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
Expand Up @@ -26,8 +26,10 @@ public void onReceive(Context context, Intent intent) {
Push.getInstance().onLocalPush(context, extras.getString("payload"), id, false);
} else {
Notification notification = intent.getParcelableExtra(context.getPackageName() + Push.DEFOLD_NOTIFICATION);
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(id, notification);
if (notification != null) {
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(id, notification);
}
}

}
Expand Down
24 changes: 14 additions & 10 deletions extension-push/src/java/com/defold/push/Push.java
Expand Up @@ -259,20 +259,22 @@ private void deleteLocalPushNotification(Context context, int uid) {
context.deleteFile(createLocalPushNotificationPath(uid));
}

private Notification getLocalNotification(final Activity activity, Bundle extras, int uid) {
Intent new_intent = new Intent(activity, PushDispatchActivity.class).setAction(Push.ACTION_FORWARD_PUSH);
private Notification getLocalNotification(final Context appContext, Bundle extras, int uid) {
Intent new_intent = new Intent(appContext, PushDispatchActivity.class).setAction(Push.ACTION_FORWARD_PUSH);
new_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
new_intent.putExtras(extras);
PendingIntent contentIntent = PendingIntent.getActivity(activity, uid, new_intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
PendingIntent contentIntent = PendingIntent.getActivity(appContext, uid, new_intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

ApplicationInfo info = activity.getApplicationInfo();
ApplicationInfo info = appContext.getApplicationInfo();

NotificationCompat.Builder builder = new NotificationCompat.Builder(activity, Push.NOTIFICATION_CHANNEL_ID)
NotificationCompat.Builder builder = new NotificationCompat.Builder(appContext, Push.NOTIFICATION_CHANNEL_ID)
.setContentTitle(extras.getString("title"))
.setContentText(extras.getString("message"))
.setContentIntent(contentIntent)
.setPriority(extras.getInt("priority"));

builder.getExtras().putInt("uid", uid);

// Find icons, if they were supplied
int smallIconId = extras.getInt("smallIcon");
int largeIconId = extras.getInt("largeIcon");
Expand All @@ -292,7 +294,7 @@ private Notification getLocalNotification(final Activity activity, Bundle extras

try {
// Get bitmap for large icon resource
PackageManager pm = activity.getPackageManager();
PackageManager pm = appContext.getPackageManager();
Resources resources = pm.getResourcesForApplication(info);
Bitmap largeIconBitmap = BitmapFactory.decodeResource(resources, largeIconId);

Expand Down Expand Up @@ -332,22 +334,24 @@ public void scheduleNotification(final Activity activity, int uid, long timestam
if (am == null) {
am = (AlarmManager) activity.getSystemService(activity.ALARM_SERVICE);
}
Intent intent = new Intent(activity, LocalNotificationReceiver.class);

Context appContext = activity.getApplicationContext();
Intent intent = new Intent(appContext, LocalNotificationReceiver.class);

Bundle extras = new Bundle();
String packageName = activity.getPackageName();
int iconSmall = activity.getResources().getIdentifier("push_icon_small", "drawable", packageName);
int iconLarge = activity.getResources().getIdentifier("push_icon_large", "drawable", packageName);
putValues(extras, uid, title, message, payload, timestampMillis, priority, iconSmall, iconLarge);

storeLocalPushNotification(activity, uid, extras);
storeLocalPushNotification(appContext, uid, extras);

intent.putExtras(extras);
intent.setAction("uid" + uid);
intent.putExtra(packageName + DEFOLD_NOTIFICATION, getLocalNotification(activity, extras, uid));
intent.putExtra(packageName + DEFOLD_NOTIFICATION, getLocalNotification(appContext, extras, uid));


PendingIntent pendingIntent = PendingIntent.getBroadcast(activity, 0, intent, PendingIntent.FLAG_ONE_SHOT);
PendingIntent pendingIntent = PendingIntent.getBroadcast(appContext, 0, intent, PendingIntent.FLAG_ONE_SHOT);
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timestampMillis, pendingIntent);
Expand Down

0 comments on commit a8a539d

Please sign in to comment.