react-native-push-notification / ios

React Native Push Notification API for iOS.
MIT License
734 stars 282 forks source link

IOS notification #306

Open raaidkh-tsa opened 3 years ago

raaidkh-tsa commented 3 years ago

Code added in onNotification automatically runs when notification is recived

import PushNotification from 'react-native-push-notification' import PushNotificationIOS from '@react-native-community/push-notification-ios' import { Linking, Platform } from 'react-native' import { ScreenNames, Server } from './src/global' import React from 'react' import { useNavigation } from '@react-navigation/native' import Axios from 'axios'

class NotificationManager extends React.Component { configure = (userId, userType) => { PushNotification.configure({ onRegister: async function (token) { console.warn("token.token", token.token); if (userType == "Brand") { await Axios.put(${Server.BASE_URL}/brandContactInfos/${userId}/registrationToken/${token.token}) } else { await Axios.put(${Server.BASE_URL}/users/${userId}/registrationToken/${token.token}) }

        },

        onNotification: function (notification) {
            const iosURL = "youadme:/";
            const androidURL = "https://youadmeapp.com";
            const data = notification;
            let baseUrl = Platform.OS == "android" ? androidURL : iosURL;
            try {
                if (data.data.key == 'POST_LIKE') {
                    Linking.openURL(`${baseUrl}/likes/${data.data.postId}`);
                } else if (data.data.key == 'POST_COMMENT') {
                    Linking.openURL(`${baseUrl}/comments/${data.data.postId}`);
                } else if (data.data.key == 'POST_APPROVED') {
                    Linking.openURL(`${baseUrl}/posts/${data.data.postId}`);
                } else if (data.data.key == 'POST_CREATED') {
                    Linking.openURL(`${baseUrl}/rejectedPost/Pending`);
                } else if (data.data.key == 'POST_REJECTED') {
                    Linking.openURL(`${baseUrl}/rejectedPost/Rejected`);
                } else if (data.data.key == 'CAMPAIGN_APPROVED') {
                    Linking.openURL(`${baseUrl}/campaign/${data.data.campaignId}`);
                } else if (data.title == 'chat') {
                    Linking.openURL(`${baseUrl}/chat`);
                } else if (data.title == 'brand') {
                    Linking.openURL(`${baseUrl}/brand/2/youAdme`);
                } else if (data.title == 'order') {
                    Linking.openURL(`${baseUrl}/order/2`);
                }
            } catch (error) {
                console.warn(error);

            }
            notification.finish(PushNotificationIOS.FetchResult.NoData);
        },
        permissions: {
            alert: true,
            badge: true,
            sound: true,
        },
    })
}

_buildAndroidNotification = (id, title, message, data = {}, options = {}) => {
    return {
        id: id,
        autoCancel: true,
        largeIcon: options.largeIcon || "ic_launcher",
        smallIcon: options.smallIcon || "ic_launcher",
        bigText: message || '',
        subText: title || '',
        vibrate: options.vibrate || false,
        vibration: options.vibration || 300,
        priority: options.priority || "high",
        importance: options.importance || "high",
        data: data
    }
}

_buildIOSNotification = (id, title, message, data = {}, options = {}) => {
    return {
        alertAction: options.alertAction || "view",
        category: options.category || "",
        userInfo: {
            id: id,
            item: data
        }
    }
}

showNotification = (id, title, message, data = {}, options = {}) => {
    PushNotification.localNotification({
        /* Android Only Properties */
        ...this._buildAndroidNotification(id, title, message, data, options),

        /* IOS Only Properties */
        ...this._buildIOSNotification(id, title, message, data, options),

        /* Android and IOS Properties */
        title: title || "",
        message: message || "",
        playSound: options.playSound || false,
        soundName: options.soundName || 'default',
        userInteraction: false
    })
}

cancelAllLocalNotification = () => {
    if (Platform.OS === 'ios') {
        PushNotification.removeAllDeliveredNotifications()
    } else {
        PushNotification.cancelAllLocalNotifications()
    }
}

unregister = () => {
    PushNotification.unregister()
}

} export const notificationManager = new NotificationManager()

dakaii commented 2 years ago

I'm experiencing the same issue....