简体   繁体   English

如何在Cordova / Phonegap Android应用程序中使用Parse.com推送通知?

[英]How do I get Parse.com Push Notifications working in a Cordova/Phonegap Android app?

How can I use Parse.com to send Push Notifications to my Cordova 3.5.0 Android app. 如何使用Parse.com将推送通知发送到我的Cordova 3.5.0 Android应用程序。

Most posts seem to cover some aspects of my problem but not the full range (Parse/Android or Phonegap/Parse) 大多数帖子似乎涵盖了我的问题的某些方面,但不是全范围(Parse / Android或Phonegap / Parse)

I've actually solved this, but am putting the complete solution up here because I've had to use a variety of fragmented solutions and forums to find the answer, and I think Cordova/Phonegap and Parse are in increasingly popular combination, and seems to be a lot of people with similar problems. 我实际上已经解决了这个问题,但我在这里提出了完整的解决方案,因为我必须使用各种支离破碎的解决方案和论坛才能找到答案,而且我认为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. 我已经问了很多与此相似的问题并且进行了大量的谷歌搜索,我已经成功地将来自不同地方的解决方案拼凑起来。

I've built my app with Cordova 3.5.0, via the command line. 我通过命令行使用Cordova 3.5.0构建了我的应用程序。 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). 我相信这些步骤与Phonegap 3.5.0以及两者的早期版本一样,只要它的第3篇(CLI)。 I'm using Eclipse and the ADT tools from Google 我正在使用Eclipse和谷歌的ADT工具

This wont work with Phonegap Build as you have to edit the Java and XML files in the Android project 这不适用于Phonegap Build,因为您必须编辑Android项目中的Java和XML文件

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. 对于那些不知道的人,Phonegap是Adobe的Cordova发行版,非常相似,但在顶部有一些额外的功能,主要用于我认为的Phonegap Build。

For the purposes of this post you can swap Cordova for Phonegap, at least in the following steps. 出于本文的目的,您可以将Cordova换成Phonegap,至少在以下步骤中。 You'll need to do all these steps after creating the project with Cordova CLI 使用Cordova CLI创建项目后,您需要执行所有这些步骤

Cordova/Parse plugin Cordova / Parse插件

I've used the following plugin to connect Parse and Cordova. 我使用以下插件连接Parse和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. 该插件有多个版本,它已被分叉几次,但Github用户benjie版本的版本为您提供了最大程度的自动化,最大限度地减少了使用源代码弄脏的需要。 Instructions for installing are found on the Github page: 安装说明可在Github页面上找到:

Updating the Activity 更新活动

You will now need to start editing the source code. 您现在需要开始编辑源代码。

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). 找到适用于您的应用程序的主要Activity类,在Eclipse导航器的src部分中,它将位于您的主程序包中,例如com.company.myapp ,然后是Example.java文件(假设Example是您的项目名称)。 It will have been generated for you by Cordova. 它将由Cordova为您生成。

In the file add this import, it can just go after the rest of the import statements: 在文件中添加此导入,它可以在其余的import语句之后执行:

import com.parse.ParseAnalytics;

And then at the end of the onCreate method add this, to track in Parse when the user opens the app from a PN 然后在onCreate方法结束时添加此项,以便在用户从PN打开应用程序时跟踪Parse

ParseAnalytics.trackAppOpened(getIntent());

Extending the application 扩展应用程序

This final part I've got from the old Parse help forums, and has taken me the longest amount of time to solve. 我从旧的Parse帮助论坛获得了最后一部分,并且花了我最长的时间来解决。

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. 事实上,你应该测试一下,以确保你到目前为止已经做到了。

...but if you force close the app (eg on the Galaxy S2 hold down the home button, and swipe the app away) you stop the app from receiving the Push Notifications. ...但是如果您强制关闭应用程序(例如,在Galaxy S2上按住主页按钮,然后轻扫应用程序),您就会停止应用程序接收推送通知。

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: 使用以下帖子我设法让应用程序在强制关闭后接收PN:

The actual solution for me was to do the following 2 steps: 对我来说,实际的解决方案是执行以下两个步骤:

1: Add a new file called ExampleApplication.java alongside your Example.java in com.company.myapp in the src section of Eclipse. 1:在Eclipse的src部分的com.company.myapp中添加一个名为ExampleApplication.java的新文件和Example.java The file needs the following content, updated accordingly for your project (eg your package and Parse keys): 该文件需要以下内容,并根据您的项目进行相应更新(例如您的包和Parse密钥):

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: Update your AndroidManifest.xml, so that you <application> tag has the following property, alongside those it already has: 2:更新AndroidManifest.xml,以便<application>标签具有以下属性,以及它已有的属性:

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

Summary 摘要

Once you've done that, you should be able to Push Notifications to your Android application. 完成后,您应该能够将通知推送到Android应用程序。

To summarise: 总结一下:

  • Install the Phonegap/Parse plugin 安装Phonegap / Parse插件
  • Update the main Activity class 更新主Activity类
  • Extend the main Application class 扩展主Application类

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. 这可能转移到非日食项目,大多数步骤将保持几乎相同,如果任何人有关于Android Studio的任何反馈或没有IDE的建设,那么我们可以更新此答案以反映这一点。

This plugin works fine in my app 这个插件在我的应用程序中工作正常

https://github.com/benjie/phonegap-parse-plugin https://github.com/benjie/phonegap-parse-plugin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM