Git Product home page Git Product logo

Comments (16)

stinju avatar stinju commented on June 20, 2024 5

Re: onPause(), React Native changed some signatures in facebook/react-native@19a1c4c#diff-dc0f3a09c238b372cb1c27aa5f7dcbce. I pointed this plugin to RN 0.24.1 (the one I'm using), changed the calls to onPause() and onDestroy(), and things seem to be working so far - however, I haven't done a thorough inspection for any remaining dependencies on 0.17.+.

build.gradle:

// add below
allprojects {
    repositories {
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url "../../../node_modules/react-native/android"
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.facebook.react:react-native:0.24.+'     // change from 0.17.+
    compile 'com.google.android.gms:play-services-gcm:8.1.0'
    compile 'com.android.support:appcompat-v7:23.0.0'
    compile project(':react-native-system-notification')
}

BackgroundService.java:

        mReactInstanceManager.onHostPause(); // change from onPause
        mReactInstanceManager.destroy();     // change from onDestroy

from react-native-gcm-android.

udfalkso avatar udfalkso commented on June 20, 2024 1

I didn't get this error, but I believe this is probably the same issue as the others are having. The way this lib is written, it's trying to instantiate the entire react app when receiving a background notification and some of the native modules (in my case FBSDK caused most of the issues) just won't instantiate nicely in this scenario for some reason.

For most use cases, this is probably overkill. I ended up writing a very minimal background service that just shows the text of the notification via the react-native-system-notification lib and nothing more. This workaround may help you as well.

First remove com.oney.gcm from this line in your Manifest:

<service android:name=".BackgroundService"></service>

Now add BackgroundService.java to your project:

package com.myapp;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import io.neson.react.notification.NotificationAttributes;
import android.os.Bundle;
import io.neson.react.notification.Notification;

public class BackgroundService extends Service {
    private static final String TAG = "BackgroundService";

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand");

        Bundle bundle = intent.getBundleExtra("bundle");
        String body = bundle.getString("gcm.notification.body", "You have a notification");
        String subject = bundle.getString("gcm.notification.subject", "MyApp Title");

        NotificationAttributes attributes = new NotificationAttributes();
        attributes.delayed = false;
        attributes.scheduled = false;
        attributes.autoClear = true;
        attributes.inboxStyle = false;
        attributes.priority = 2;
        attributes.sound = "default";
        attributes.smallIcon = "ic_launcher";
        attributes.message = body;
        attributes.subject = subject;
        Notification notification = new Notification(this, 1, attributes);
        notification.create();

        return START_NOT_STICKY;
    }

}

from react-native-gcm-android.

gijoehosaphat avatar gijoehosaphat commented on June 20, 2024 1

Hmm, I think these may be different issues though. I have gotten other errors similar to #54.

I had to rearrange my client entry js to ensure none of my App would be imported or registered if we were receiving a notification. Like this:

'use strict'

import React from 'react-native'
import GcmAndroid from 'react-native-gcm-android'
import Notification from 'react-native-system-notification'

if (GcmAndroid.launchNotification) {
  Notification.create({
    subject: GcmAndroid.launchNotification.title,
    message: GcmAndroid.launchNotification.body,
  })
  GcmAndroid.stopService()
} else {
  //We don't want the app loading up as normal when the app receives
  //a notification. So we only load this when the app launches as normal.
  const App = require('./containers/App').default
  React.AppRegistry.registerComponent('WhyWait', () => App)
}

from react-native-gcm-android.

gijoehosaphat avatar gijoehosaphat commented on June 20, 2024

I get this same error. The notification does appear, and can be clicked on. But the app crashes in the background. The result of this is that you cannot receive a follow up notification until the user launches/closes the app again.

[email protected]
[email protected]
[email protected]

from react-native-gcm-android.

peterlazar1993 avatar peterlazar1993 commented on June 20, 2024

@stinju Can you elaborate a little more, I tried changing those but my build failed with
/Users/pete/Code/Play/React-Native/Awesome/node_modules/react-native-gcm-android/android/src/main/java/com/oney/gcm/BackgroundService.java:47: error: cannot find symbol mReactInstanceManager.onHostPause(); ^ symbol: method onHostPause() location: variable mReactInstanceManager of type ReactInstanceManager /Users/pete/Code/Play/React-Native/Awesome/node_modules/react-native-gcm-android/android/src/main/java/com/oney/gcm/BackgroundService.java:48: error: cannot find symbol mReactInstanceManager.destroy(); ^ symbol: method destroy() location: variable mReactInstanceManager of type ReactInstanceManager 2 errors

from react-native-gcm-android.

stinju avatar stinju commented on June 20, 2024

@peterlazar1993 were you able to resolve your issue? Looks like your build wasn't compiling against an RN version with the new signatures. Check to see if the 'onHostPause' method is implemented in ReactInstanceManager.java in your RN version. If so, then you might need to fiddle with the changes I mentioned above to get your build to use your RN version for this package. BTW, I wasn't clear above, but the build.gradle file mentioned is the one for react-native-gcm-android, not the one for your project/app.

from react-native-gcm-android.

jojonarte avatar jojonarte commented on June 20, 2024

Did you resolve this already? How did you solve this issue?

from react-native-gcm-android.

peterlazar1993 avatar peterlazar1993 commented on June 20, 2024

@jojonarte We decided to write a custom BackgroundService

from react-native-gcm-android.

vmakhaev avatar vmakhaev commented on June 20, 2024

I resolved this error by creating a fork where removed not existing methods. And another thing is you should not require whole App while in background service, I'm not sure why, but maybe because some environment is not initialised.

from react-native-gcm-android.

peterlazar1993 avatar peterlazar1993 commented on June 20, 2024

@vmakhaev What kind of background service did you write? Care to share?

from react-native-gcm-android.

vmakhaev avatar vmakhaev commented on June 20, 2024

@peterlazar1993 I used the one from react-native-gcm-android

from react-native-gcm-android.

savioseb avatar savioseb commented on June 20, 2024

Comment the
GcmAndroid.stopService();

line.

It should work.

My index.android.js snippet:

`
if (GcmAndroid.launchNotification) {
console.log('Before entering function:', GcmAndroid.launchNotification);
//GcmAndroid.stopService();
console.log('GcmAndroid.launchNotification:', GcmAndroid.launchNotification);
var notification = GcmAndroid.launchNotification;
var info = JSON.parse(notification.info);
Notification.create({
subject: info.subject,
message: info.message,
});

//GcmAndroid.stopService();
} else {
class Savio extends Component {
`

from react-native-gcm-android.

savioseb avatar savioseb commented on June 20, 2024

#22 also addressed by doing this change.

from react-native-gcm-android.

samcarlosimpres avatar samcarlosimpres commented on June 20, 2024

@udfalkso Your solution works, but there's any way to make it show the message received by an external API instead of a static one??

from react-native-gcm-android.

udfalkso avatar udfalkso commented on June 20, 2024

@samcarlosimpres Yes. The code fetches the message from the notification, the static string there is just a fallback.

For me the value is coming through in the "gcm.notification.body" key of the bundle, perhaps for you it is at another key.

bundle.getString("gcm.notification.body", "You have a notification");

from react-native-gcm-android.

openGeeksLab avatar openGeeksLab commented on June 20, 2024

guys, sorry.
But how to receive notification when app is fully closed on Android?

from react-native-gcm-android.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.