Android上的离子推送通知不会在Ionic.io上注册令牌 [英] Ionic push notifications on Android doesn't register token on Ionic.io

查看:67
本文介绍了Android上的离子推送通知不会在Ionic.io上注册令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android上使用ionic实现推送通知。

I want to implement push notification on Android using ionic.

我在devdactic的教程中遵循了Ionic push的文档 Devdactic推送通知android 我在平台上看到的那个例子被保存了一个令牌。

I've followed the documentation from Ionic push in a tutorial from devdactic Devdactic push notifications android I see with that example in the platform is saved a token.

我做了我需要的所有设置包括GCM服务和注册用户到离子平台,但没有注册令牌。

I've make all settings that I need including GCM service and register user to ionic platform, but no token is registered.

我在模拟器中运行app并注册用户但没有保存令牌。在一些修改器之后,我在控制台中收到一个令牌,但是不行。

I run app in emulator and user is registered but no token is saved. After some modifiers I receive a token in console but is not ok.

在示例中,令牌不同,推送不起作用。有人根据上一篇文档有一个Ionic Push的工作示例吗?

In example token is different and push does't work. Does someone have an working example with Ionic Push based on last documentation?

推荐答案

这是我用来注册推送的,它是凌乱但希望可以有所帮助。它检查当前用户是否经过身份验证,如果不是,则使用UUID(我使用UUID生成器插件)对其进行签名并保存令牌。只需确保你的应用程序设置了Ionic.io,这应该有效:)

this is what I use to register pushes, it is messy but hopefully can be of some use. It checks if the current user is authenticated, if they aren't then it signs them up with a UUID (i used a UUID generator plugin) and saves the token. Just make sure your app is set up with Ionic.io and this should work :)

var user = Ionic.User.current();

if (user.isAuthenticated()) {

            var push = new Ionic.Push({
                "debug": true,
                "onNotification": function (notification) {                        
                },
                "onRegister": function (data) {
                    console.log(data.token);
                    return true;
                },
                "pluginConfig": {
                    "android": {
                        "icon": "icon"
                    },
                    "ios": {
                        "badge": true,
                        "sound": true,
                        "alert": true
                    }
                }
            });
        } else {


            var uid = uuid2.newuuid();

            var details = {
                'email': uid + '@example.com',
                'password': 'secretpassword'
            };

            Ionic.Auth.signup(details).then(function () {
                var options = { 'remember': true };
                Ionic.Auth.login('basic', options, details).then(function () {
                    user = Ionic.User.current();
                    user.set('uid', uid);
                    user.save();

                    var push = new Ionic.Push({
                        "debug": true,
                        "onNotification": function (notification) {                                
                        },
                        "onRegister": function (data) {
                            console.log(data.token);
                            return true;
                        },
                        "pluginConfig": {
                            "android": {
                                "icon": "icon"
                            },
                            "ios": {
                                "badge": true,
                                "sound": true,
                                "alert": true
                            }
                        }
                    });


                    push.register(function (token) {
                        console.log("Device token:", token.token);
                        push.saveToken(token);
                    });
                }, function () { });
            }, function () { });

        }

这篇关于Android上的离子推送通知不会在Ionic.io上注册令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆