如何让 Parse.com 推送通知在 Cordova/Phonegap Android 应用程序中工作? [英] How do I get Parse.com Push Notifications working in a Cordova/Phonegap Android app?

查看:23
本文介绍了如何让 Parse.com 推送通知在 Cordova/Phonegap Android 应用程序中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Parse.com 向我的 Cordova 3.5.0 Android 应用程序发送推送通知.

大多数帖子似乎涵盖了我的问题的某些方面,但不是全部(Parse/Android 或 Phonegap/Parse)

Most posts seem to cover some aspects of my problem but not the full range (Parse/Android or Phonegap/Parse)

我实际上已经解决了这个问题,但是我把完整的解决方案放在这里,因为我不得不使用各种零散的解决方案和论坛来找到答案,我认为 Cordova/Phonegap 和 Parse 在越来越流行的组合,似乎很多人都有类似的问题.

推荐答案

我问过很多与此类似的问题,并进行了大量的谷歌搜索,我已经设法从不同的地方拼凑出一个解决方案.

I've asked a variety of questions similar to this and done lots of googling, and I've managed to piece together a solution from a variety of places.

我已经通过命令行使用 Cordova 3.5.0 构建了我的应用程序.我相信这些步骤与 Phonegap 3.5.0 一样适用,并且可能是两者的早期版本,只要它是 post 3 (CLI).我正在使用 Eclipse 和 Google 的 ADT 工具

I've built my app with Cordova 3.5.0, via the command line. I believe these steps will work just as well with Phonegap 3.5.0, and probably earlier versions of both as long as it's post 3 (CLI). I'm using Eclipse and the ADT tools from Google

这不适用于 Phonegap Build,因为您必须编辑 Android 项目中的 Java 和 XML 文件

对于那些不知道的人,Phonegap 是 Adob​​e 的 Cordova 发行版,非常相似,但在顶部有一些附加功能,我认为主要用于 Phonegap Build.

For those that don't know, Phonegap is Adobe's distribution of Cordova, and is very similar but has some additional functionality on top, mostly for Phonegap Build I think.

就本文而言,您可以至少在以下步骤中将 Cordova 换成 Phonegap.使用 Cordova CLI 创建项目后,您需要执行所有这些步骤

For the purposes of this post you can swap Cordova for Phonegap, at least in the following steps. You'll need to do all these steps after creating the project with Cordova CLI

Cordova/Parse 插件

我使用以下插件连接 Parse 和 Cordova.该插件有多个版本,它已经分叉了几次,但 Github 用户 benjie 的版本为你做了最多的自动化,最大限度地减少了你动手的需要源代码.安装说明可在 Github 页面上找到:

I've used the following plugin to connect Parse and Cordova. There are multiple versions of the plugin, it's been forked a few times but the version by Github user benjie's version does the most amount of automation for you, minimizing the need to get your hands dirty with the source code. Instructions for installing are found on the Github page:

  • https://github.com/benjie/phonegap-parse-plugin (no longer maintained - try a different fork)

更新活动

您现在需要开始编辑源代码.

You will now need to start editing the source code.

为您的应用程序找到主 Activity 类,在 Eclipse 导航器的 src 部分,它将位于您的主包中,类似于 com.company.myapp然后是 Example.java 文件(假设 Example 是您的项目名称).它将由 Cordova 为您生成.

Find the main Activity class for your app, in the src section of the Eclipse navigator it'll be in your main package, something like com.company.myapp and then the Example.java file (assuming Example is your project name). It will have been generated for you by Cordova.

在文件中添加这个导入,它可以在剩下的导入语句之后:

In the file add this import, it can just go after the rest of the import statements:

import com.parse.ParseAnalytics;

然后在 onCreate 方法的末尾添加这个,以便在用户从 PN 打开应用程序时在 Parse 中进行跟踪

And then at the end of the onCreate method add this, to track in Parse when the user opens the app from a PN

ParseAnalytics.trackAppOpened(getIntent());

扩展应用

最后一部分是我从旧的 Parse 帮助论坛上得到的,我花了最长的时间来解决.

This final part I've got from the old Parse help forums, and has taken me the longest amount of time to solve.

如果您将应用保留为当前状态,您可以收到推送通知.事实上,你应该测试一下,以确保到目前为止你做对了.

If you leave the app as it is currently, you can receive Push notifications. In fact you should test that to make sure you've done it right so far.

...但是如果您强行关闭应用程序(例如在 Galaxy S2 上按住主页按钮,然后将应用程序滑开),您将阻止应用程序接收推送通知.

...but if you force close the app (e.g. on the Galaxy S2 hold down the home button, and swipe the app away) you stop the app from receiving the Push Notifications.

我相信这是因为您通过强制关闭应用程序(包括 PN 的侦听器)来扼杀应用程序的各个方面.

I believe this is because you're killing every aspect of the app by forcing it closed including the listener for the PNs.

使用以下帖子,我设法让应用在强制关闭后接收 PN:

Using the following posts I managed to get the app to receive PNs after forcing it closed:

对我来说实际的解决方案是执行以下两个步骤:

The actual solution for me was to do the following 2 steps:

1:在 中的 com.company.myapp 中,在您的 Example.java 旁边添加一个名为 ExampleApplication.java 的新文件Eclipse 的 src 部分.该文件需要以下内容,并针对您的项目进行相应更新(例如您的包和解析键):

1: Add a new file called ExampleApplication.java alongside your Example.java in com.company.myapp in the src section of Eclipse. The file needs the following content, updated accordingly for your project (e.g. your package and Parse keys):

package com.company.myapp;

import android.app.Application;
import android.content.Context;

import com.parse.Parse;
import com.parse.ParseInstallation;
import com.parse.PushService;

import com.company.myapp.Example;

public class ExampleApplication extends Application 
{
    private static ExampleApplication instance = new ExampleApplication();

    public ExampleApplication() {
        instance = this;
    }

    public static Context getContext() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        // register device for parse
        Parse.initialize(this, "APP_KEY", "CLIENT_KEY");
        PushService.setDefaultPushCallback(this, Example.class);
        ParseInstallation.getCurrentInstallation().saveInBackground();
    }
}

2:更新您的 AndroidManifest.xml,以便您的 标签具有以下属性,以及它已有的属性:

2: Update your AndroidManifest.xml, so that you <application> tag has the following property, alongside those it already has:

android:name="com.company.myapp.ExampleApplication"

总结

完成此操作后,您应该能够将通知推送到您的 Android 应用程序.

Once you've done that, you should be able to Push Notifications to your Android application.

总结:

  • 安装 Phonegap/Parse 插件
  • 更新主Activity类
  • 扩展主应用程序类

这可能可以转移到非 Eclipse 项目,大部分步骤将保持几乎相同,如果有人对 Android Studio 或在没有 IDE 的情况下构建有任何反馈,那么我们可以更新此答案以反映这一点.

This is probably transferable to non-eclipse projects, most of the steps will remain almost identical and if any one has any feedback regarding Android Studio or building without an IDE then we could update this answer to reflect that.

这篇关于如何让 Parse.com 推送通知在 Cordova/Phonegap Android 应用程序中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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