Headless task kills itself when app is killed - android

My service kills itself when the app is killed/closed.
Here's the background service I'm implementing (bgMessaging.js):
import firebase from 'react-native-firebase';
// Optional flow type
import type { RemoteMessage } from 'react-native-firebase';
export default async (message: RemoteMessage) => {
// handle your message
console.log("RemoteMessage", message);
const newNotification = new firebase.notifications.Notification()
.android.setSmallIcon(message.data.icon)
.android.setChannelId(message.data.channel_id)
.android.setAutoCancel(true)
.android.setBigText(message.data.body)
.setNotificationId(message.messageId)
.setBody(message.data.body);
if(message.data.title != undefined)
newNotification.setTitle(message.data.title);
if(message.data.color != undefined)
newNotification.android.setColor(message.data.color);
if(message.data.bigPicture != undefined)
newNotification.android.setBigPicture(message.data.bigPicture);
if(message.data.largeIcon != undefined)
newNotification.android.setLargeIcon(message.data.largeIcon);
firebase.notifications().displayNotification(newNotification);
return Promise.resolve();
}
Here's my index.js:
import { AppRegistry } from 'react-native';
import App from './app';
import bgMessaging from './bgMessaging';
const app = new App();
AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => bgMessaging);
According to the documentation of react-native-firebase, this implementation should work. However, RNFirebaseBackgroundMessage service kills itself when the app is killed/closed.
Is this due to the implementation of react-native-navigation? This is an exact example of my implementation of react-native-navigation.
Using react-native#0.53.3 and react#16.2.0
Please help.

Related

AWS Pinpoint opts out Android users of pushnotifications by default in react native

I've tried everything to get push notifications for Android to work with Pinpoint in my react native app.
The iOS integration works as expected.
In android, i'm able to record the endpoint, but when I export the endpoint from pinpoint, I can see that the field that controls push notification--OptOut--is set to ALL.
This is the integration that I have in my App.js to record the endpoints:
import React, {Component} from 'react';
import { StyleSheet, View, PushNotificationIOS, AsyncStorage, Text } from 'react-native'
//aws
import Amplify, { Auth } from 'aws-amplify'
import aws_exports from './aws-exports'
import PushNotification from '#aws-amplify/pushnotification'
Analytics.configure(aws_exports);
Amplify.configure(aws_exports)
console.log('exports',aws_exports)
PushNotification.configure(aws_exports);
type Props = {};
class App extends Component<Props> {
componentDidMount(){
console.log(PushNotification)
// get the notification data when notification is received
PushNotification.onNotification((notification) => {
// Note that the notification object structure is different from Android and IOS
console.log('in app notification', notification);
// required on iOS only (see fetchCompletionHandler docs: https://facebook.github.io/react-native/docs/pushnotificationios.html)
notification.finish(PushNotificationIOS.FetchResult.NoData);
});
// get the registration token
// This will only be triggered when the token is generated or updated.
PushNotification.onRegister((token) => {
console.log('in app registration', token);
});
}
render() {
...
}
}
export default codePush(codePushOptions)(App)

React-native-firebase notification in background (Android)

I am using the react-native-firebase library and I am brainstorming to customize my notifications when they are in the background, I tried to follow the documentation but to no avail, are there native code .java to handle notifications?
To customize the background notifications you can add in the index of your application:
import { AppRegistry } from 'react-native'
import App from './App'
import { backgroundMessageNotificationHandler } from 'src/common/notifications'
AppRegistry.registerComponent('testApp', () => App)
AppRegistry.registerHeadlessTask(
'RNFirebaseBackgroundMessage',
() => backgroundMessageNotificationHandler)
The backgroundMessageNotificationHandler could be something like that:
export const backgroundMessageNotificationHandler = async (message: any) => {
const localNotification = new firebase.notifications.Notification().android
.setChannelId(androidNotificationsChannel.channelId)
.android.setSmallIcon('iconBlackAndWhite')
.android.setLargeIcon('iconBlackAndWhite')
.android.setPriority(firebase.notifications.Android.Priority.High)
.setNotificationId(message.messageId)
.setSound('default')
.setTitle(message.data.title)
.setBody(message.data.body)
.setData(message.data)
if (Platform.OS === 'android') {
createChannel()
}
firebase.notifications().displayNotification(localNotification)
return Promise.resolve()
}
Remember to follow the steps to setting up the build.gralde, MainActivity and AndroidManifest. You can follow the steps specified in the following post:
React-Native Android Push Notification via Firebase

Headless task in react native android appliation

I am currently using the headless task to receive a push notification and show the local notification. I am currently using react-native-firebase to integrate firebase cloud messaging.
export default bgMessaging = (message) => {
console.log('hello', message)
return Promise.resolve();
}
This is the piece of code which I am using to create task.
And in index.js
AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => bgMessaging);
I am using this to registerHeadlessTask.
But when the app is in the killed state I am not getting any console message and don't if my task is running.
Need help to know how can I debug the background task and show custom notification for the killed state.
I solved it as follows:
// index.js
import { AppRegistry, Alert } from 'react-native';
import messaging from '#react-native-firebase/messaging';
import App from './App';
import { name as appName } from './app.json';
messaging().setBackgroundMessageHandler(async remoteMessage => {
console.log('Message handled in the background!', remoteMessage);
});
//receive the message in the background
messaging().onMessage(async teste => {
console.log('teste:', teste);
const novo = (title, message) => {
return Alert.alert(title, message);
};
novo(teste.notification.title, teste.notification.body);
});
AppRegistry.registerComponent(appName, () => App);
This is because we have to validate when the push message arrives in the background in the app

How to run code in both foreground and background in React Native - Android?

I'm doing an task for listening message (even in background) and show it on UI.
I have done in listening messages real time. But if the app is in background, it doesn't receive messages in that time.
I ask for any ideas, helps.
I searched HeadlessJS, react-native-background-task but not work for me.
Actually, i want to run this app even in Background and Foreground.
Thank you for advanced.
It's my code until now
index.android.js
//Import libraries
import React, { Component } from 'react';
import {
AppRegistry,
Text
} from 'react-native';
import SmsListener from 'react-native-android-sms-listener';
//Main component
export default class Main extends Component {
state = {
lastMessage: ''
}
//Receive messages
listen = SmsListener.addListener(message => {
this.setState({ lastMessage: message.body });
console.log('message');
})
//render UI
render() {
return (
<Text> Message is: {this.state.lastMessage} </Text>
);
}
}
//Register component
AppRegistry.registerComponent('ReadMessageApp', () => Main);

App is not handling push if app is in background (React Native)

I'm facing a really troubling issue with my React Native app and Pushwoosh.
If I close my app completely and send a notification from Pushwoosh control panel, it appears, I tap on it on my phone and my app receive push info.
But if my app is in background (for example, I've already opened it and just press "Home" button on my phone) and I send the notificacion from control panel, it appears, I tap on it BUT my app won't receive any push data.
It seems like onPushHandler function is being unregistered if my app is in background.
Here is my code (I removed a lot of useless code for this purpose):
import React, {
Component,
} from 'react';
import {
AppRegistry,
} from 'react-native';
class App extends Component {
constructor(props) {
super(props);
this.state = {};
this.pushHandler = this.pushHandler.bind(this);
}
componentDidMount() {
Pushwoosh.init(/* myconfig */);
Pushwoosh.register(
(token) => {
console.log('✓ Registered for pushes');
},
(error) => {
console.error('Failed to register: ' + error);
}
);
Pushwoosh.onPushOpen(this.pushHandler);
}
render() {
return (
<View><Text>Test</Text></View>
);
}
pushHandler(pushData) {
console.log(pushData);
Pushwoosh.onPushOpen(this.pushHandler);
}
}
AppRegistry.registerComponent('MyApp', () => App);

Categories

Resources