我如何获得Parse.com推送通知在科尔多瓦/ PhoneGap的Andr​​oid应用程序的工作? [英] How do I get Parse.com Push Notifications working in a Cordova/Phonegap Android app?

查看:134
本文介绍了我如何获得Parse.com推送通知在科尔多瓦/ PhoneGap的Andr​​oid应用程序的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Parse.com发送推送通知我科尔多瓦3.5.0 Android应用程序。

大多数职位似乎包括我的问题的某些方面,但不是全部的范围(解析/ Android或的PhoneGap /解析)

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

其实我已经解决了这个,但我把完整的解决方案,在这里,因为我不得不使用各种零散的解决方案和论坛中找到了答案,我认为科尔多瓦/ PhoneGap的和解析是日益流行的组合,并且似乎有很多人有类似的问题。

推荐答案

我问过各种与此类似,并做了很多谷歌搜索的问题,我已经成功地拼凑从不同的地方解决。

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.

我已经建立了我与科尔多瓦3.5.0应用程序,通过命令行。我相信这些措施将工作同样出色的PhoneGap 3.5.0,而且很可能两者的更早版本,只要它的后3(CLI)。我使用Eclipse和从谷歌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的不会工作作为构建你在Android项目编辑Java和XML文件

对于那些不知道,PhoneGap的是科尔多瓦Adobe的分布,是非常相似,但顶部有一些附加的功能,多为PhoneGap的生成,我认为。

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的,至少在下面的步骤。你需要与科尔多瓦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

科尔多瓦/解析插件

我用下面的插件连接解析和科尔多瓦。有多个版本的插件,它已经分叉了几次,但该版本通过Github上用户的 benjie 的的版本做了最大量的自动化为您,尽量减少需要得到你的手脏与来源$ C ​​$ C。安装说明被发现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)

更新活动

您现在需要开始编辑源$ C ​​$ C。

You will now need to start editing the source code.

查找您的应用程序中的主要活动类,在的src 的Eclipse的导航部分,它会在你的主包,像 com.company.myapp <​​/ STRONG>然后 Example.java 文件(假设的示例的是您的项目名称)。它已经生成了您科尔多瓦。

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打开应用程序

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());

扩展应用程序

这最后一部分,我从老解析帮助论坛得到的,并已采取了我的时间来解决的最长时间。

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.

...但如果​​你强行关闭应用程序(例如在银河S2按住home键,并刷卡应用程序以外)你收到推送通知停止的应用程序的。

我相信这是因为你被强制关闭它包括的侦听器期票杀应用程序的每一个方面。

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:

  • https://www.parse.com/questions/push-notification-in-android-while-app-closed
  • https://www.parse.com/questions/cannot-send-push-to-android-after-app-is-closed-until-screen-unlock

对我来说,实际的解决方案是做了以下2个步骤:

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

1:添加 ExampleApplication.java 称为一个新的文件旁边的 Example.java 融为一体。 company.myapp <​​/ STRONG>在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:更新您的Andr​​oidManifest.xml,这样你&lt;应用&GT; 标记具有以下属性,沿着那些它已经具备:

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"

摘要

一旦你做到了这一点,你应该能够推送通知到你的Andr​​oid应用程序。

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

要总结一下:

  • 安装的PhoneGap /解析插件
  • 更新主Activity类
  • 展开主应用程序类

这是可能转移到非Eclipse项目,大多数步骤将保持几乎相同,如果任何人有关于Android的工作室或建筑物没有一个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推送通知在科尔多瓦/ PhoneGap的Andr​​oid应用程序的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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