I am new to Rails. I am trying to send push notification using rpush gem.
I am following these steps:
n = Rpush::Gcm::Notification.new
n.app = Rpush::Gcm::App.find_by_name("android_app")
n.registration_ids = ["#{d.device_token}"]
n.data = { title: "#{self.title}"}
n.save!
The notification gets saved but is not getting delivered, and I get the following error description:
"Unable to deliver notification 10, received error (Failed to deliver to all recipients. Errors: NotRegistered.)"
Not able to understand the issue here. Please help me.
Thanks!
When you find above mentioned message in your log, means, as said #abielita and rpush wiki documentation, the identification_id GCM is using to send notification, is no more valid.
Device with (d.device_token) as registration token, is no more registered on GCM (Firebase) Service.
This message come for a specified reason: adapt your code to this condition.
Maybe is better destroy your "d" record !?
Becomes helpful Rpush.reflect in config/initializers/rpush.rb
Uncomment event notification on.gcm_invalid_registration_id and add your code inside block
e.g.
on.gcm_invalid_registration_id do |app, error, registration_id|
Device.where(registration_id: registration_id).take.destroy
end
and encloses your sending notification code inside a conditional check
unless(d.nil?)
n = Rpush::Gcm::Notification.new
n.app = Rpush::Gcm::App.find_by_name("android_app")
n.registration_ids = ["#{d.device_token}"]
n.data = { title: "#{self.title}"}
n.save!
end
I hope it help
Related
i have succesfully integrating react-native-fcm and
i am getting notification when i am sending from firebase console (in foreground,background,and killed state of app)
but when i am sending it from our server with custom data i cannot recieved when app is in (killed,background) state
i have also tried content-availble:true in data object
below is the notification data which i am sending
{
aps = {
"content-available" = 1;
};
body = "Get a free T-Shirt #WROGN on every purchase via Shopholix. T&C apply.";
"gcm.message_id" = "0:1475746605785619%9a4a567a9a4a567a";
"gcm.notification.data" = "{\"image\":\"http:\\/\\/res.cloudinary.com\\/saurabh\\/image\\/upload\\/v1469791885\\/media\\/yljgxgmfg1qbulxzwakm.png\",\"user_type\":\"all\",\"screen\":\"store\",\"id\":\"56d7e33ce69f7c8f06550002\",\"title\":\"Shopholix\",\"message\":\"Get a free T-Shirt #WROGN on every purchase via Shopholix. T&C apply.\",\"body\":\"Get a free T-Shirt #WROGN on every purchase via Shopholix. T&C apply.\"}";
"gcm.notification.priority" = high;
id = 56d7e33ce69f7c8f06550002;
image = "http://res.cloudinary.com/saurabh/image/upload/v1469791885/media/yljgxgmfg1qbulxzwakm.png";
message = "Get a free T-Shirt #WROGN on every purchase via Shopholix. T&C apply.";
screen = store;
title = Shopholix;
"user_type" = all;
}
Any help would be appreciated
Thanks,
Your content_available is set to 1. I'm pretty sure when using FCM, you should use true or false. Correct me if I'm reading your payload structure in a wrong way, but are you specifying the content_available separate from the GCM payload?
Also, the priority parameter should be outside the notification payload parameter.
See here for more details.
Were you able to resolve your issue?
Looking at the JSON, if it is exactly how you're sending it, then it is not well formed. All of the keys need to be strings surrounded by quotes. And each key-value pair needs to be comma-separated, not semi-colon.
Remote iOS client successfully connects to me, send subscribe command (it works fine), but on "unsubscribe" command I get next error:
Unsubscribing from channel: {"channel":"Assessor::StationChannel", "station_id": 1}
Could not execute command from {"command"=>"unsubscribe", "identifier"=>"{\"channel\":\"Assessor::StationChannel\", \"station_id\": 1}"}) [NoMethodError - undefined method `unsubscribe_from_channel' for nil:NilClass]: /app/vendor/bundle/ruby/2.2.0/gems/actioncable-5.0.0/lib/action_cable/connection/subscriptions.rb:44:in `remove_subscription' | /app/vendor/bundle/ruby/2.2.0/gems/actioncable-5.0.0/lib/action_cable/connection/subscriptions.rb:40:in `remove' | /app/vendor/bundle/ruby/2.2.0/gems/actioncable-5.0.0/lib/action_cable/connection/subscriptions.rb:16:in `execute_command' | /app/vendor/bundle/ruby/2.2.0/gems/actioncable-5.0.0/lib/action_cable/connection/base.rb:88:in `dispatch_websocket_message' | /app/vendor/bundle/ruby/2.2.0/gems/actioncable-5.0.0/lib/action_cable/server/worker.rb:58:in `block in invoke'
Subscribe message format:
{"command": "subscribe", "identifier": "{\"channel\":\"Assessor::StationChannel\", \"station_id\": 1}"}
Unsubscribe message format:
{"command": "unsubscribe", "identifier": "{\"channel\":\"Assessor::StationChannel\", \"station_id\": 1}"}
I cannot reproduce this problem on localhost, so maybe somebody can help me?
I saw a similar error. I was trying to unsubscribe via the client (JS). I eventually figured out it was because the javascript to .remove(subscription) takes the subscription and not the subscription identifier.
This is how I got it to work without error. Perhaps it will help you find out why you are getting the error from the server side.
subscription = App.cable.subscriptions.subscriptions[0]
App.cable.subscriptions.remove(subscription);
(Note, I'm just pulling the first subscription from the array, TODO: Search for the subscription I want to remove)
Here is the bug I was seeing and how I eventually found the source code/answer. I ran these from the webclient console:
App.cable.subscriptions.create({channel: "RoomChannel", room_id: 2})
That line works and I get a "... is transmitting the subscription confirmation" on stdout for rails s
App.cable.subscriptions.remove({channel: "RoomChannel", room_id: 2})
That line blows up, yells at my kids, and insults my wife which looks like:
[NoMethodError - undefined method `unsubscribe_from_channel' for nil:NilClass]: /usr/local/lib64/ruby/gems/2.3.0/gems/actioncable-5.0.0.1/lib/action_cable/connection/subscriptions.rb:44:in `remove_subscription'
I also noted the following line before the crash.
Unsubscribing from channel:
The code to produce that is: logger.info "Unsubscribing from channel: #{data['identifier']}". Which means it wasn't finding the data['identifier']. So I started debugging and I see that line 88 of base.rb in actioncable only gets {"command":"unsubscribe"} and not something like {"command":"unsubscribe", "identifier":" (channel name here)}
Which brought me to action_cable.js. (I would have started here, but I hate JS.). Here was my problem: function(subscription). I was sending the identifier and not the subscription object.
Subscriptions.prototype.remove = function(subscription) {
this.forget(subscription);
if (!this.findAll(subscription.identifier).length) {
this.sendCommand(subscription, "unsubscribe");
}
return subscription;
};
App.cable.subscriptions.create({channel: "RoomChannel", room_id: 2}) returns a subscription object you have to pass that into the remove function
var subscription = App.cable.subscriptions.create({channel: "RoomChannel", room_id: 2});
Then later
App.cable.subscriptions.remove(subscription);
After several tries I eventually figured it out. Late reply but this worked for me hope it does for you.
App["identifier"].disconnected() #e.g App["chat_4"].disconnect()
=> YourChannel stopped streaming from chat_4 #rails terminal
Above is the line to stop the streaming and below is how you subscribe to the channel
App['chat' + id] = App.cable.subscriptions.create({channel:
'YourChannel', chat_id: id}, {
disconnected: function () {
App.cable.subscriptions.remove(this)
},
)}
I know this is an old question, but I suffered from the same issue. None of the answers above worked for me and in fact, at least in Rails 5.0.1, they are incorrect. It is a frontend issue all right, but here's why it doesn't matter whether you call App.yourChannelName.unsubscribe() or App.yourChannelName.remove(App.yourChannelName)
Eg. if you have something like this (example code is in coffeescript, so ignore the lack of vars and other stuff from vanilla JS):
App.yourChannel = App.cable.subscriptions.create({channel: 'YourChannel', id: id})
...
// do stuff, execute callbacks, whatnot
...
// try to execute unsubscribe
App.yourChannel.unsubscribe()
The .unsubscribe() is a method on Subscription prototype which only does return this.consumer.subscriptions.remove(this)
The this.consumer.subscriptions returns the instance of your subscription, in example above, it would be App.yourChannel and calls .remove method with the instance of Subscription - ie. with App.yourChannel
So App.yourChannel.unsubscribe() is the same as calling App.cable.subscriptions.remove(App.yourChannel) (or whichever variable you choose to store the instance of Subscription in.
I have also been seeing the same error as OP, except that in my case, it was caused by App.yourChannel.unsubscribe() being called two times - the first time it was called immediately after I received specific data via the channel and the second time was due to a custom cleanup being run in a specific scenario before the App.yourChannel was re-subscribed.
So if you see a similar error, I suggest you look at the server logs.
You'll probably see something like
Registered connection (some-id) <-- initial subscription
YourChannel is transmitting the subscription confirmation
...
// other stuff while user is subscribed to the channel
...
Unsubscribing from channel: {"channel":"YourChannel","id":"some-id"} <-- initial unsubscribe call
YourChannel stopped streaming from your_channel_some-id
// some other requests potentially, perhaps some DB queries
...
// there are no requests to subscribe to the channel with the some-id, eg. you won't see this
// Registered connection (some-id)
// YourChannel is transmitting the subscription confirmation
Unsubscribing from channel: {"channel":"YourChannel","id":"some-id"} <-- duplicated unsubscribe call
Could not execute command from {"command"=>"unsubscribe", "identifier"=>"{\"channel\":\"YourChannel\",\"id\":\"some-id\"}"}) [NoMethodError - undefined method `unsubscribe_from_channel' for nil:NilClass]:
Basically, the user subscribes, unsubscribes, then tries to unsubscribe again (even though they are not subscribed to that channel anymore)
Are there any documentations regarding the data field of the Push Notification message i mean n.data { }. what does it mean?
I seem to be having the same issue as well
This is my code
n = Rpush::Apns::Notification.new
n.app = Rpush::Apns::App.find_by_name("Loanstreet Partners")
n.device_token = #user_auth_token.device_token # 64-character hex string
n.alert = "New Case Submission: " + la.case_code
n.data = { foo: :bar }
n.badge = 1
n.save!
Rpush.push
Rpush.apns_feedback
In my rpush_notifications table, i see the notification created but in my rpush_feedback it is empty. not sure why it doesn't get sent. Anyone has a clue?
I don't get any error or anything as well. So I'm sort of confused.
I do however get a Notification received from Ionic log but i dont see any notification.
I'm trying to use Houston to send a push notification. I recreated the provisioning profile, can upload a device token successfully, but when I push using apn the console shows no readout. Is there a way to log what APN is doing? (Submitted a github issue; posting here hoping someone's ran into this.
Running this code returns nothing. I've tried removing spaces & <> from the device token too, same result.
Using rspec 3.1.0
Using simplecov-html 0.8.0
require 'houston'
# Environment variables are automatically read, or can be overridden by any specified options. You can also
# conveniently use `Houston::Client.development` or `Houston::Client.production`.
APN = Houston::Client.production #tried development, same result
APN.certificate = File.read("/Users/quantum/Documents/cliqupprodcerts.pem")
# An example of the token sent back when a device registers for notifications
token = "tokentoken"
# Create a notification that alerts a message to the user, plays a sound, and sets the badge on the app
notification = Houston::Notification.new(device: token)
notification.alert = "Hello, World!"
# Notifications can also change the badge count, have a custom sound, have a category identifier, indicate available Newsstand content, or pass along arbitrary data.
notification.badge = 57
notification.sound = "sosumi.aiff"
notification.category = "INVITE_CATEGORY"
notification.content_available = true
notification.custom_data = {foo: "bar"}
# And... sent! That's all it takes.
v = APN.push(notification)
puts v
~
EDIT: I had tried production before, production & development don't work. puts notification.errors after sendings shows nothing.
First you should check whether you have had a develop profile that has a permission to push remote notification.
Then if your device is iOS 8.0 or later ,"Please use registerForRemoteNotifications and registerUserNotificationSettings: instead");
The last, you can do follow the steps of "houston"
I have a trouble making notification hub to work.
If I send notification to tag that has only one registration associated with it, then everything's fine. Here's source:
var alert = "{\"aps\":{\"alert\":\"some message(targeted)\"},\"inAppMessage\": \"text\"}";
hub.SendAppleNativeNotificationAsync(alert,"mytag").ContinueWith(t => {
var outcome = t.Result;
Console.WriteLine(outcome);
});
But if I try to sent my notification to all users, like so:
var alert = "{\"aps\":{\"alert\":\"some message(all users)\"},\"inAppMessage\": \"text\"}";
hub.SendAppleNativeNotificationAsync(alert).ContinueWith(t => {
var outcome = t.Result;
Console.WriteLine(outcome);
});
then no one gets the notification. I checked monitor in Azure portal but there were no errors present.
Do you guys have some ideas?
Behavior you described is incorrect. There is one known issue which can potentially cause such kind of errors for Apple only:
If some registrations in the Notification Hub have invalid device token (it typically happens when application switches from test APNS to production or if some fake tokens were used during testing in emulator) then APNS terminates connection during the send and all notifications being sent after that using same connection are just ignored. Right now Notification Hub does not handle this situation correctly.
Way to fix - clean up "bad" registrations. If you are about developing/testing and it is not critical to lose the data - just remove all the registrations. If you have a lot of existing users then email me and we will try to find solution for your particular case.
I had same issue and I tried to set true EnableTestSend property, but no luck. Then I just removed the tags while send notifications. It worked like charm.
NotificationOutcome outcome = null;
var alert = "{\"aps\":{\"alert\":\"" + "From " + user + ": " + message + "\",\"sound\":\"default\"}}";
outcome = await Notifications.Instance.Hub.SendAppleNativeNotificationAsync(alert);