Push Notification not working in foreground IOS - objective-c

push notification is working perfectly fine when app is in background, the main issue arises when the app is in foreground. I am able to receive the payload but the notification is not shown in the notification banner
payload received below
{"data": {"fcm_options": {"image": "https://importduniya.com/dealer/particular/TrustyCane.jpg"}}, "messageId": "1591459535622805", "mutableContent": true, "notification": {"body": "Test123", "ios": {}, "title": "Test"}, "sentTime": "1591459535"}
I think the below function is not getting called when the app is in foreground
//Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
NSLog(#"will-present");
completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}

You should implement application:didReceiveRemoteNotification:fetchCompletionHandler: as well

Related

React native push notification not shown when app is closed

I have a react native app. I am using react-native-push-notification npm module for push notifications (mainly local notifications). This is working fine.
I use OneSignal service for remote notifications which works fine as well.
However, now I am trying to use react-native-freshchat-sdk (third party tool) to provide chat functionality within the app.
I have the following code
PushNotification.configure({
// (optional) Called when Token is generated (iOS and Android)
onRegister: function (token: any) {
console.log('TOKEN:', token)
dispatch(tokenGenerated(token));
},
//(required) Called when a remote or local notification is opened or received
onNotification: function (notification: any) {
console.log(`**********notification received ********************* : ${JSON.stringify(notification)}`)
Freshchat.isFreshchatNotification(notification.data, (freshchatNotification: any) => {
if (freshchatNotification) {
console.log(`**********freshchatNotification received *********************`)
Freshchat.handlePushNotification(notification.data);
} else {
console.log(`**********some other notification*********************`)
}
})
},
Things work fine when the app is in the foreground or background. I do get remote notifications from FreshChat with the chat content. FreshChat uses FCM for sending notifications.
However, when the app is closed or killed, I do not get the notifications (atleast they are not shown in the tray)
When I open the chat, I can see the chat message there.
Chat remote notification looks like the following
{
"foreground": false,
"userInteraction": false,
"id": "-1665678567",
"data": {
"channel_id": "748134",
"app_id": "5787643203",
"notif_type": "1",
"msg_alias": "8343f922-9dfa-4fe9-9220b-61af8902e7650",
"source": "freshchat_user",
"body": "heelo world message",
"timestamp": "1660956855495",
"user_name": "Foo Bar",
"target_user_alias": "ssd1232-75df-87f0-fsdsd",
"conv_id": "6479825358242"
}
}
I have been scratching my head to understand why I am not getting remote notifications when the app is closed. What's confusing me even more is that I get OneSignal remote notifications even when the app is closed. I would like to see similar behaviour with the remote notification sent by FreshChat
Any help pointers would be really handy. I have been stuck with these for days now and have no clue what's going wrong.
Thanks in advance

Remote push notification heads-up not working in react native when app is in background state (Android)

I have integrated push notification using Firebase library. Below library specifications,
"#react-native-firebase/messaging": "^10.5.0",
"react-native": "0.63.4",
When app is in background state push notification is received but it's not pop up on screen.
What setting i need to do to heads-up push notification when app is in background state.
Try using firebase console or postman to send notification, if you still face problem to get notification then your sdk is not setup correctly.for postman use this link
https://fcm.googleapis.com/fcm/send
and paramenters:
{
"to" : "your_token",
"collapse_key" : "type_a",
"notification" : {
"body" : "Body of Your Notification",
"title": "Title of Your Notification"
}
}
and headers:
Authorization:server_key
Content-Type:application/json

Expo push notifications listener not firing

I'm implementing push notifications in an app, but I need to handle the notification to navigate to a specific screen. The problem is that my listener never fires, I searched a lot on google and tried a lot of things but none of them worked..
I'm using AWS SNS together with FCM to send the push.
What I have is this in my App.tsx:
componentDidMount() {
Notifications.addListener(notification => {
alert("IT WORKED")
})
}
But ofcourse this alert is never called..
I read this issue https://github.com/expo/expo/issues/4183 and it seems that the format of the push matters, so right now I'm sending the push in this format:
{
"GCM": "{ \"notification\": { \"message\": \"Sample message for Android endpoints\", \"title\": \"Hello You...\" } }"
}
And I receive the push, but when I click on it to open the app, doesn't fire my listener, but in the issue the guy said to send like this:
{
"GCM": "{ \"data\": { \"message\": \"Sample message for Android endpoints\", \"title\": \"Hello world...\", \"body\": \"msg body\", \"experienceId\": \"#expousername/app-slug\" } }"
}
But if I send like this, I don't even receive the push.
What I'm doing wrong?

Xcode Recieve notification only when app is active

I write in Objective-C.
I'm using remote push up notification for app control. I don't want to disturb users with alerts and sounds when app is not active. What i need to do - if app is not active - do nothing when notification is recieved.
Thank you.
You want to send silent notifications. A notification sent without an aps key is silent. iOS will not play a sound, nor will it display a banner on the device. Your app will still receive such notifications, if it's running, and can respond as it chooses. If the app is in the background, it can display a local notification, or it can just ignore it.
Here are palyload structure which you need to receive for your case:
{
"aps" : {
"badge" : 5,
},
"acme1" : "bar",
"acme2" : [ "bang", "whiz" ]
}
BTWL: you can not mention badge as well, if you don't need it.
just add below code into your Appdelegate.m file
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
UIApplicationState state = [application applicationState];
if(state == UIApplicationStateActive)
{
//app in active mode
// add your code as you want to handle
}
else{
// app is in terminated mode
}
}
It was easier than i thought. Just no alert and sound elements in array in section aps.
No alert - nothing to show.
No sound - nothing to play.

How handle silent notifications in iOS

I need to fetch data from parse SDK and update in core data after receiving push notification(silent notification)in foreground and background.how to receive silent push notification and update data(without on click of notification).Here my code is
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
/*
//notification data format from server
{ "operation": "Update",
“Element”: “1234”,
"action": "db" }
//after receiving update notification ,i should fetch that unique element(1234) data from parse and save data into database.
*/
if ([[userInfo objectForKey:#"operation"] isEqualToString:#"Update"])
{
NSLog(#"update notication called");
//fetching data and updating database using updateDatabase method ,its implemented in some other class.
[database updateDatabase:[userInfo objectForKey:#“Element”]];
}
}
this is silent notification, So how to handle silent notification in iOS. thanks
you have to enable the background mode for that. Click on your Project Name in the file inspector, then select your target and select the tab "Capabilities". Now turn "Background Modes" on and select "Push Notifications".
Modify your payload and make sure you've content-available set to '1'. This tag is required for silent notification. Without this the payload will be a non-silent notification.
{
aps: {
content-available: 1,
sound: "default"
}
}