cmcewen / react-native-gcm-ios

iOS GCM for React Native
MIT License
15 stars 3 forks source link

sending notification with gcmToken #2

Closed siemya closed 8 years ago

siemya commented 8 years ago

İ imported the module and i can successfully retrieve a gcmToken. İs this really the only available feature of this module, or can i send a notification with the help of react-native-push-notification.

This is my request which returns success, but it doesn't work.

curl --header "Authorization: key=xxxxxxxxxxxx" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send -d "{\"registration_ids\":[\"xxxxxxxxxxxx\"],\"data\":{\"message\":\"Hello\"}}

cmcewen commented 8 years ago

The only purpose of this module is to fetch the gcm tokens, but you should be able to see a notification display without changing any other code if your app is in the background. I'm personally using this library: https://github.com/ToothlessGear/node-gcm

It looks like you're hitting the wrong endpoint and you need a notification payload: https://developers.google.com/cloud-messaging/ios/client#receive_messages_through_the_gcm_apns_interface http://stackoverflow.com/questions/31109514/making-gcm-work-for-ios-device-in-the-background

siemya commented 8 years ago

I made a post request according to the link you sent. The first response is success but notification didn't show, the next requests are failure with message "NotRegistered", don't know why because the token doesn't change which i fetch on app init (i log it on 'register' and it is always the same). Also isn't it possible to get the payload object with PushNotificationIOS's notification listener, so that i will be aware of the notification when my app is running on foreground?

cmcewen commented 8 years ago

Try setting the notification to high priority. And yes you should be able to use the PushNotificationIOS listener, but if you want to go cross platform I'd recommend that other library. As for the NotRegistered error, I have never seen that

siemya commented 8 years ago

It doesn't seem to work. After i deploy my app to my phone and get a new token, i make a request from postman and i get a successful response, but no notification on my screen. When i do the same request again i start to get a failure response with "NotRegistered" error, but the same token continues to come on 'register' event. If i delete the app and build it again the same scenario occurs.

Postman Request

https://gcm-http.googleapis.com/gcm/send
Content-Type:application/json
Authorization: key=XXXXXXXXXX

{
 "to" : "XXXXXXXXXX",
 "notification": {
   "sound": "default",
   "badge": "2",
   "title": "default",
   "body": "Test Push!"
  },
  "priority": "high",
  "content_available": true
}

AppDelegate.m

// Required to register for notifications
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
  [RCTPushNotificationManager didRegisterUserNotificationSettings:notificationSettings];
}
// Required for the register event.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
  [RCTPushNotificationManager didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
// Required for the notification event.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification
{
  [RCTPushNotificationManager didReceiveRemoteNotification:notification];
}
// Required for the localNotification event.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
  [RCTPushNotificationManager didReceiveLocalNotification:notification];
}

React-Native Code

import React, {PushNotificationIOS} from "react-native";
import GcmIOS from "react-native-gcm-ios"; 
//i get "GcmIOS.addListener is not a function" when i use require
var PushNotification = require('react-native-push-notification');

componentWillMount() {
     PushNotification.requestPermissions();
     GcmIOS.addListener('register', this.registerDevice.bind(this));
     PushNotificationIOS.addEventListener('notification', this.notificationReceived.bind(this));
}

componentWillUnmount() {
     GcmIOS.removeEventListener('register', this.registerDevice.bind(this));
     PushNotificationIOS.removeEventListener('notification', this.notificationReceived.bind(this));
}

registerDevice(gcmToken) {
     console.log(gcmToken);
     pushNotificationService.registerDevice(gcmToken);
}

notificationReceived(notification = {})  {
    this.setState({notification: notification});
}
cmcewen commented 8 years ago

Do you have the APNS provisioning profile uploaded? http://stackoverflow.com/questions/35488888/gcm-ios-device-not-registered

siemya commented 8 years ago

it was a provisioning problem, thanks :)