b8ne / react-native-pusher-push-notifications

Manage pusher interest subscriptions and notification events in Javascript.
MIT License
97 stars 85 forks source link

Update Android to use Pusher Beams SDK #33

Closed mikebridge closed 5 years ago

mikebridge commented 5 years ago

Update to the pusher beams Client SDK

closes #32

NOTES:

robwalkerco commented 5 years ago

@mikebridge I'm hoping to use your changes in this PR, but I'm not sure if there are any particular setup steps. It would be great if you could take a look at adding some content to https://github.com/b8ne/react-native-pusher-push-notifications/blob/master/README.md#android :)

mikebridge commented 5 years ago

@robwalkerco This is from the diff of when we initially pulled this in, but I'm not sure all of it is necessary. I can clean this up and add it to the PR:

Add the temporary URL to package.json:

"dependencies": {
    "react-native-pusher-push-notifications": "git+http://git@github.com/ZeptInc/react-native-pusher-push-notifications#2.1.0"
}

Add this to android/build.gradle:

buildscript {
    // ...
    dependencies {
        // ...
        // Add this line
        classpath 'com.google.gms:google-services:4.0.1'
    }
}    

Add this to android/app/build.gradle:

dependencies {
   compile project(':react-native-pusher-push-notifications')
   // ...
   implementation 'com.google.firebase:firebase-messaging:17.1.0'
   implementation 'com.pusher:push-notifications-android:1.0.2'
}

// Add this line to the end of the file
apply plugin: 'com.google.gms.google-services'

Set up android/app/google-services.json (not sure what all is required for this)

Add RNPusherPushNotificationsPackage to MainApplication.java:

import com.b8ne.RNPusherPushNotifications.RNPusherPushNotificationsPackage;
// ...

protected List<ReactPackage> getPackages() {
  return Arrays.<ReactPackage>asList(     
    new MainReactPackage(),
    new RNPusherPushNotificationsPackage(), // <--- add this
    // ...
}

Add to android/settings.gradle (below rootProject.name)

include ':react-native-pusher-push-notifications'
project(':react-native-pusher-push-notifications').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-pusher-push-notifications/android')

Then from typescript:

import {Platform} from "react-native";
import {PUSHER_BEAMS_INSTANCE_ID} from "react-native-dotenv";
import RNPusherPushNotifications from "react-native-pusher-push-notifications";

const init= (): void => {
    RNPusherPushNotifications.setInstanceId(PUSHER_BEAMS_INSTANCE_ID);

    RNPusherPushNotifications.on("notification", handleNotification);
    RNPusherPushNotifications.setOnSubscriptionsChangedListener(onSubscriptionsChanged);
};

const subscribe = (interest: string): void => {
    console.log(`Subscribing to "${interest}"`);
    RNPusherPushNotifications.subscribe(
        interest,
        (statusCode, response) => {
            console.error(statusCode, response);
        },
        () => {
            console.log(`CALLBACK: Subscribed to ${interest}`);
        }
    );
};

const handleNotification = (notification: any): void => {
    console.log(notification);
    if (Platform.OS === "ios") {
        console.log("CALLBACK: handleNotification (ios)");
    } else {
        console.log("CALLBACK: handleNotification (android)");
        console.log(notification);
    }
};

const onSubscriptionsChanged = (interests: string[]): void => {
    console.log("CALLBACK: onSubscriptionsChanged");
    console.log(interests);
}
robwalkerco commented 5 years ago

Thanks. I’ll try to get it setup tomorrow using your PR and what you’ve provided here.

robwalkerco commented 5 years ago

Thanks @mikebridge. Seems to be working ok so far :)

The only other install step that's required is to add import com.b8ne.RNPusherPushNotifications.RNPusherPushNotificationsPackage; to MainApplication.java

mikebridge commented 5 years ago

@robwalkerco Thanks, I've made that edit to the post.

alvincrespo commented 5 years ago

Hi all! I'd love to setup Android with this package. Anything I can do to help get this in?

alvincrespo commented 5 years ago

@mikebridge @robwalkerco Did you run into this error?

> Task :app:compileDebugJavaWithJavac FAILED
/Users/alvincrespo/workspace/echobind/truecoach/TruecoachMessenger/android/app/src/main/java/com/truecoachmessenger/MainApplication.java:16: error: package com.b8ne.RNPusherPushNotifications does not exist
import com.b8ne.RNPusherPushNotifications.RNPusherPushNotificationsPackage;
                                         ^
/Users/alvincrespo/workspace/echobind/truecoach/TruecoachMessenger/android/app/src/main/java/com/truecoachmessenger/MainApplication.java:30: error: cannot find symbol
        , new RNPusherPushNotificationsPackage()
              ^
  symbol: class RNPusherPushNotificationsPackage
2 errors

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
mikebridge commented 5 years ago

@alvincrespo I haven't seen it before but I wonder if I'm missing a step to trigger the installation of the library via gradle, e.g. yarn build

alvincrespo commented 5 years ago

@mikebridge I actually just figured it out. I had to add the following to android/app/build.gradle

dependencies {
    compile project(':react-native-pusher-push-notifications')
}
mikebridge commented 5 years ago

@alvincrespo Sorry, that looks like an oversight on my part---I'll add that to the instructions

alvincrespo commented 5 years ago

@mikebridge Thanks for updating the docs! This is super awesome work. 👍

johankladder commented 5 years ago

First of all, this pull request fixed push notifications for me when running react-native run-android! The thing is, when I generate a .apk for release/debug or do react-native run-android --variant=release I get the following message:

TypeError: undefined is not an object (evaluating 's.getInitialNotification')

The app crashes with this error in release, but it registered and subscribed successfully. When I send the notification from the command line , it is showed correctly.

Is there someone with the same problem or did I configure something wrong?

Update

Okay, this was a local error. I was calling subscribe and register functions before that app was loaded. Thanks anyway! ❤

stephenkiers commented 5 years ago

Any reason this hasn't been merged yet?

mikebridge commented 5 years ago

I updated my fork to push-notifications-android:1.0.2. I encountered the error Could not find firebase-iid-interop.aar, which I fixed by applying this.

I'm tagging a new release, 2.2.0.

opula commented 5 years ago

This works great except I am getting double notifications on android whereas iOS works fine. Is anyone else experiencing this issue?

mikebridge commented 5 years ago

@opula I haven't seen that occur on android---do you have some code that reproduces it?

opula commented 5 years ago

@mikebridge A sample would be a bit hard right now since I ripped out the old pusher code to replace it with this new package.

I can tell you that I called the setInstanceId on the main component constructor but do not call subscribe until after a user logs in. For your project, where did you call both methods?

If need be, I'll strip out the extra code for a simple sample.

opula commented 5 years ago

I've been able to resolve the problem. Everything now is working as expected. Along the way, I ran into two issues:

1) Double Notifications - This was because there were some legacy items in my AndroidManifest.xml from the previous pusher notifications implementation. Once those were removed the notifications worked as expected.

2) Error compiling on debug - I was getting an error that I could not parse google-services.json. This is because the package name in the google-services.json file must match the app and in debug mode a ".DEBUG" was added.

Just thought I would add in those two issues in case anyone else ran into them.

mikebridge commented 5 years ago

@opula Great, glad you were able to figure it out!

siers commented 5 years ago

@mikebridge Hi!

Shouldn't it the main description have this change?

-    "react-native-pusher-push-notifications": "git+http://git@github.com/ZeptInc/react-native-pusher-push-notifications#2.1.0"
+    "react-native-pusher-push-notifications": "git+http://git@github.com/ZeptInc/react-native-pusher-push-notifications#v2.2.0"
gprocell927 commented 5 years ago

By chance, did you happen to work with the app icon badge count after using this change for Android? I've been struggling with getting that piece to work without digging into/interfering with this part of it.

mikebridge commented 5 years ago

@gprocell927 I don't think there is a badge count available on Android. On ios you can update the badge with the number of notifications, but on Android you just get a little dot.

mikebridge commented 5 years ago

@siers I had some trouble updating to RN 0.58 on that branch and pushed some versions higher than they probably should be. I haven't had time to circle back and make a cleaner release.

emilkarl commented 5 years ago

How is it going? Is there anything I can help with?

emilkarl commented 5 years ago

Got this working on RN 0.59 with some changes @mikebridge.

  1. I my projects build gradle image

  2. in this repo image

emilkarl commented 5 years ago

It would be awesome to get this merged! Based on v2.3.0-alpha

Can someone review it and see what needs to be done?

@mikebridge @b8ne @siers @opula @stephenkiers

mikebridge commented 5 years ago

I'm just testing out a new version with push-notifications-android 1.4.0. The problems we were having seem to be related to firebase-messaging:17.6.0

emilkarl commented 5 years ago

Yes you need to use 17.3.4 I think

mikebridge commented 5 years ago

17.5 seems to work so far.

emilkarl commented 5 years ago

Nice