Skip to content

Do not show system tray on iOS when a new notification is received and the application is in foreground #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Mar 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
</config-file>

<framework src="com.parse:parse-android:1.15.7" />
<framework src="com.parse:parse-android:1.16.7" />
<framework src="com.android.support:support-v4:+" />
<framework src="me.leolin:ShortcutBadger:1.1.17@aar" />

Expand Down
30 changes: 19 additions & 11 deletions src/android/ParsePushPluginReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import android.app.NotificationManager;

import github.taivo.parsepushplugin.ParsePushConfigReader;
import github.taivo.parsepushplugin.ParsePushConfigException;

import android.support.v4.app.NotificationCompat;

Expand All @@ -29,10 +28,6 @@

import android.content.SharedPreferences;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;

import me.leolin.shortcutbadger.ShortcutBadger;

public class ParsePushPluginReceiver extends ParsePushBroadcastReceiver {
Expand Down Expand Up @@ -64,11 +59,17 @@ protected void onPushReceive(Context context, Intent intent) {
// If the user wants multiple notifications in the tray, then we let ParsePushBroadcastReceiver
// handle it from here
super.onPushReceive(context, intent);
} else {
// use tag + notification id=0 to limit the number of notifications in the tray
// (older messages with the same tag and notification id will be replaced)
NotificationManager notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.notify(getNotificationTag(context, intent), 0, getNotification(context, intent));
}
else {
// check if this is a silent notification
Notification notification = getNotification(context, intent);

if (notification != null) {
// use tag + notification id=0 to limit the number of notifications in the tray
// (older messages with the same tag and notification id will be replaced)
NotificationManager notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.notify(getNotificationTag(context, intent), 0, notification);
}

//
// A user with Android 5.0.1 reports that notif is not created in tray when
Expand Down Expand Up @@ -145,6 +146,9 @@ protected Notification getNotification(Context context, Intent intent) {

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

// check if this is a silent notification
boolean isSilent = !pnData.has("title") && !pnData.has("alert");

if (pnData.has("title")) {
builder.setTicker(pnData.optString("title")).setContentTitle(pnData.optString("title"));
} else if (pnData.has("alert")) {
Expand Down Expand Up @@ -187,7 +191,11 @@ protected Notification getNotification(Context context, Intent intent) {
builder.setColor(context.getResources().getColor(colorId));
}

return builder.build();
if (!isSilent) {
return builder.build();
}

return null;
}

private static JSONObject getPushData(Intent intent) {
Expand Down
12 changes: 9 additions & 3 deletions src/ios/ParsePushPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,20 @@ -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNoti
UIApplication *application = [UIApplication sharedApplication];
[self jsCallback:notification.request.content.userInfo withAction:(application.applicationState == UIApplicationStateActive) ? @"RECEIVE" : @"OPEN"];

completionHandler(UNNotificationPresentationOptionAlert);
// do not show notifications into the system tray if the application is in foreground
if (application.applicationState == UIApplicationStateActive) {
completionHandler(UNNotificationPresentationOptionNone);
}
else {
completionHandler(UNNotificationPresentationOptionAlert);
}
}

-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
NSLog(@"User info %@", response.notification.request.content.userInfo);

[self jsCallback:response.notification.request.content.userInfo withAction: @"OPEN"];

completionHandler();
}

Expand Down