在离子应用中实现推送通知 [英] implementing push notification in ionic app

查看:111
本文介绍了在离子应用中实现推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Android的离子应用中实现推送通知。
我已按照 http:// docs的分步指南进行操作.ionic.io / V1.0 /文档/按压从划伤
当我在Android手机上运行我的应用程序时,注册用户列在apps.ionic.io中。所以用户注册工作正常。
但设备注册无效。
给出错误无法读取属性'pushNotification'未定义

I am trying to implement push notification in ionic app for Android. I have followed step by step guide from http://docs.ionic.io/v1.0/docs/push-from-scratch. When I am running my app on android phone, then the registered users are listed in apps.ionic.io. So user registration is working fine. But device registration is not working. It is giving error Cannot read property 'pushNotification' of undefined

这是我在app.js顶部的代码

This is my code at the top of app.js

angular.module('starter', ['ionic','ngCordova',
    'ionic.service.core',
    'ionic.service.push',
    'starter.controllers',
    'starter.services'])

    .config(['$ionicAppProvider', function($ionicAppProvider) {
        // Identify app
        $ionicAppProvider.identify({
            // The App ID (from apps.ionic.io) for the server
            app_id: '',
            // The public API key all services will use for this app
            api_key: '',
            // Set the app to use development pushes
           // dev_push: true
            gcm_id: ''
        });
    }])

以下是我的控制器中的代码

Here is the code in of my controller

 .controller('DashboardCtrl', function($scope,$localstorage, WildfireService, CommonUtilityService,PushNotificationService,$ionicPopup, $ionicLoading) {      
      PushNotificationService.identifyUser();
      PushNotificationService.pushRegister();

    })

这是我的services.js

Here is my services.js

 .service('PushNotificationService', function($q, $ionicUser, $ionicPush) {
        var PushNotificationService = this;
        PushNotificationService.identifyUser  = function(){
            var user = $ionicUser.get();
            if(!user.user_id) {
                // Set your user_id here, or generate a random one.
                user.user_id = $ionicUser.generateGUID();
            };

            // Add some metadata to your user object.
            angular.extend(user, {
                name: 'Technews',
                bio: 'Hardcoded for now'
            });

            // Identify your user with the Ionic User Service
            $ionicUser.identify(user).then(function(){
                //alert('Identified user ' + user.name + '\n ID ' + user.user_id);
                return true;
            });
        },

        PushNotificationService.pushRegister = function(){
            // Register with the Ionic Push service.  All parameters are optional.
            $ionicPush.register({
                canShowAlert: true, //Can pushes show an alert on your screen?
                canSetBadge: true, //Can pushes update app icon badges?
                canPlaySound: true, //Can notifications play a sound?
                canRunActionsOnWake: true, //Can run actions outside the app,
                onNotification: function(notification) {
                    // Handle new push notifications here
                    // console.log(notification);
                    alert(notification);
                    return true;
                }
            });
        }
    })

任何人都可以告诉我错误在哪里或我缺少什么?

Can any one tell me where is the error or what I am missing?

我在index.html中添加了这些

I have added these in index.html

<script src="lib/ionic/js/ionic.bundle.js"></script>
    <script src="lib/ngCordova/dist/ng-cordova.js"></script>
    <script src="lib/ionic-service-core/ionic-core.js"></script>
    <script src="lib/ionic-service-push/ionic-push.js"></script>


推荐答案

最后推送通知对我有用。我将pushRegister的函数调用从控制器移动到identifyUser函数。所以这是我的新工作代码。
控制器代码

Finally push notification is working for me. I moved the function call of pushRegister from controller to identifyUser function. So here is the new working code for me. Controller code

.controller('DashboardCtrl', function($scope,$localstorage, WildfireService, CommonUtilityService,PushNotificationService,$ionicPopup, $ionicLoading) {      
      PushNotificationService.identifyUser();


    })

这是新服务.js

 .service('PushNotificationService', function($q, $ionicUser, $ionicPush) {
            var PushNotificationService = this;
            PushNotificationService.identifyUser  = function(){
                var user = $ionicUser.get();
                if(!user.user_id) {
                    // Set your user_id here, or generate a random one.
                    user.user_id = $ionicUser.generateGUID();
                };

                // Add some metadata to your user object.
                angular.extend(user, {
                    name: 'Technews',
                    bio: 'Hardcoded for now'
                });

                // Identify your user with the Ionic User Service
                $ionicUser.identify(user).then(function(){
                    //alert('Identified user ' + user.name + '\n ID ' + user.user_id);
PushNotificationService.pushRegister();
                    return true;
                });
            },

            PushNotificationService.pushRegister = function(){
                // Register with the Ionic Push service.  All parameters are optional.
                $ionicPush.register({
                    canShowAlert: true, //Can pushes show an alert on your screen?
                    canSetBadge: true, //Can pushes update app icon badges?
                    canPlaySound: true, //Can notifications play a sound?
                    canRunActionsOnWake: true, //Can run actions outside the app,
                    onNotification: function(notification) {
                        // Handle new push notifications here
                        // console.log(notification);
                        alert(notification);
                        return true;
                    }
                });
            }
        })

这篇关于在离子应用中实现推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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