Android:如何使用Parse作为替代GCM推送通知提供程序?

Android:如何使用Parse作为替代GCM推送通知提供程序?,第1张

概述在问题的最后看SEITEDIT#2(Google更新了push的实现方式,因此更容易处理gcm和解析)我已经在应用程序中使用GCM,我想添加解析作为替代方案.这就是我现在拥有的(所有权限都被正确声明):<serviceandroid:name="com.mypackagename.GCMIntentService"android:enabled="true"

在问题的最后看SEIT EDIT#2(Google更新了push的实现方式,因此更容易处理gcm和解析)

我已经在应用程序中使用GCM,我想添加解析作为替代方案.这就是我现在拥有的(所有权限都被正确声明):

<service    androID:name="com.mypackagename.GCMIntentService"    androID:enabled="true" /><receiver    androID:name="com.Google.androID.gcm.GCMbroadcastReceiver"    androID:permission="com.Google.androID.c2dm.permission.SEND" >   <intent-filter>       <action androID:name="com.Google.androID.c2dm.intent.RECEIVE" />       <action androID:name="com.Google.androID.c2dm.intent.REGISTRATION" />       <category androID:name="com.mypackagename.GCMIntentService" />   </intent-filter></receiver>

‘GCMIntentService'(继承自’GCMBaseIntentService’)类处理服务器注册和接收消息 – 它工作正常并且接收到所有推送消息.服务器有时会发送自定义数据,因此我自己处理消息并以编程方式创建通知(单击通知时使用的意图具有从服务器发送的一些重要附加功能).

我想以某种方式使解析行为以相同的方式,以便能够从解析网站发送频道推送并创建我自己的通知,但我尝试的一切都失败(遵循AndroID推送教程不是真的为我的问题工作).是否有人尝试过类似的东西?在花了很多时间调整推送指南/教程之后,我有点想法 – 有时候我没有收到任何通知;有时解析和我的接收器都被调用,我得到双重通知.我也尝试使用解析REST APIs注册并自己处理所有内容,但发现它在AndroID上是不可能的.

那么,我怎样才能处理解析推送和传统的gcm推送(使用我的服务器),这样我就可以访问这两个通知,并且我可以从头开始构建它们(使用所需的附加功能创建我自己的待处理通知)?

编辑#1:

我尝试的第一件事是使用解析服务并有一个广播接收器来处理GCM消息:

AndroIDMaifest.xml:

<service     androID:name="com.mypackagename.GCMIntentService"     androID:enabled="true" /><service androID:name="com.parse.PushService"    androID:enabled="true"/><receiver     androID:name="com.Google.androID.gcm.GCMbroadcastReceiver"     androID:permission="com.Google.androID.c2dm.permission.SEND" >        <intent-filter>        <action androID:name="com.Google.androID.c2dm.intent.RECEIVE" />        <action androID:name="com.Google.androID.c2dm.intent.REGISTRATION" />        <category androID:name="com.mypackagename.GCMIntentService" />    </intent-filter></receiver>

并且解析库需要以下初始化:

Parse.initialize(context, appID, APIKey);PushService.setDefaultPushCallback(context, MainActivity.class);// I'm subscribing to channel push because I send channel pushes from the// parse consolePushService.subscribe(context, MDConstants.PARSE_PUSH_CHANNEL, MainActivity.class);ParseInstallation.getCurrentInstallation().saveInBackground();

问题是我收到了来自我的其他提供者的通知但是我没有从parse收到任何内容(声明了所有权限)并且我从解析库中收到以下错误(当收到错误时,解析注册没有正确完成 – 我在解析控制台中看不到我的设备:

E/com.parse.ManifestInfo(11677): Cannot use GCM for push because the app manifest is missing some required declarations. Please make sure that these permissions are declared as children of the root <manifest> element:E/com.parse.ManifestInfo(11677): E/com.parse.ManifestInfo(11677): <uses-permission androID:name="androID.permission.INTERNET" />E/com.parse.ManifestInfo(11677): <uses-permission androID:name="androID.permission.ACCESS_NETWORK_STATE" />E/com.parse.ManifestInfo(11677): <uses-permission androID:name="androID.permission.VIBRATE" />E/com.parse.ManifestInfo(11677): <uses-permission androID:name="androID.permission.WAKE_LOCK" />E/com.parse.ManifestInfo(11677): <uses-permission androID:name="androID.permission.GET_ACCOUNTS" />E/com.parse.ManifestInfo(11677): <uses-permission androID:name="com.Google.androID.c2dm.permission.RECEIVE" />E/com.parse.ManifestInfo(11677): <permission androID:name="com.mypackagename.permission.C2D_MESSAGE" androID:protectionLevel="signature" />E/com.parse.ManifestInfo(11677): <uses-permission androID:name="com.mypackagename.permission.C2D_MESSAGE" />E/com.parse.ManifestInfo(11677): E/com.parse.ManifestInfo(11677): Also, please make sure that these services and broadcast receivers are declared as children of the <application> element:E/com.parse.ManifestInfo(11677): E/com.parse.ManifestInfo(11677): <service androID:name="com.parse.PushService" />E/com.parse.ManifestInfo(11677): <receiver androID:name="com.parse.GcmbroadcastReceiver" androID:permission="com.Google.androID.c2dm.permission.SEND">E/com.parse.ManifestInfo(11677):   <intent-filter>E/com.parse.ManifestInfo(11677):     <action androID:name="com.Google.androID.c2dm.intent.RECEIVE" />E/com.parse.ManifestInfo(11677):     <action androID:name="com.Google.androID.c2dm.intent.REGISTRATION" />E/com.parse.ManifestInfo(11677):     <category androID:name="com.mypackagename" />E/com.parse.ManifestInfo(11677):   </intent-filter>E/com.parse.ManifestInfo(11677): </receiver>E/com.parse.PushService(11677): TrIEd to use push, but this app is not configured for push due to: Push is not configured for this app because the app manifest is missing required declarations. Please add the following declarations to your app manifest to support either GCM or PPNS for push (or both). To enable GCM support, please make sure that these permissions are declared as children of the root <manifest> element:E/com.parse.PushService(11677): E/com.parse.PushService(11677): <uses-permission androID:name="androID.permission.INTERNET" />E/com.parse.PushService(11677): <uses-permission androID:name="androID.permission.ACCESS_NETWORK_STATE" />E/com.parse.PushService(11677): <uses-permission androID:name="androID.permission.VIBRATE" />E/com.parse.PushService(11677): <uses-permission androID:name="androID.permission.WAKE_LOCK" />E/com.parse.PushService(11677): <uses-permission androID:name="androID.permission.GET_ACCOUNTS" />E/com.parse.PushService(11677): <uses-permission androID:name="com.Google.androID.c2dm.permission.RECEIVE" />E/com.parse.PushService(11677): <permission androID:name="com.mypackagename.permission.C2D_MESSAGE" androID:protectionLevel="signature" />E/com.parse.PushService(11677): <uses-permission androID:name="com.mypackagename.permission.C2D_MESSAGE" />E/com.parse.PushService(11677): E/com.parse.PushService(11677): Also, please make sure that these services and broadcast receivers are declared as children of the <application> element:E/com.parse.PushService(11677): E/com.parse.PushService(11677): <service androID:name="com.parse.PushService" />E/com.parse.PushService(11677): <receiver androID:name="com.parse.GcmbroadcastReceiver" androID:permission="com.Google.androID.c2dm.permission.SEND">E/com.parse.PushService(11677):   <intent-filter>E/com.parse.PushService(11677):     <action androID:name="com.Google.androID.c2dm.intent.RECEIVE" />E/com.parse.PushService(11677):     <action androID:name="com.Google.androID.c2dm.intent.REGISTRATION" />E/com.parse.PushService(11677):     <category androID:name="com.mypackagename" />E/com.parse.PushService(11677):   </intent-filter>E/com.parse.PushService(11677): </receiver>E/com.parse.PushService(11677): To enable PPNS support, please make sure that these permissions are declared as children of the root <manifest> element:E/com.parse.PushService(11677): E/com.parse.PushService(11677): <uses-permission androID:name="androID.permission.INTERNET" />E/com.parse.PushService(11677): <uses-permission androID:name="androID.permission.ACCESS_NETWORK_STATE" />E/com.parse.PushService(11677): <uses-permission androID:name="androID.permission.RECEIVE_BOOT_COMPLETED" />E/com.parse.PushService(11677): <uses-permission androID:name="androID.permission.VIBRATE" />E/com.parse.PushService(11677): <uses-permission androID:name="androID.permission.WAKE_LOCK" />E/com.parse.PushService(11677): E/com.parse.PushService(11677): Also, please make sure that these services and broadcast receivers are declared as children of the <application> element:E/com.parse.PushService(11677): E/com.parse.PushService(11677): <service androID:name="com.parse.PushService" />E/com.parse.PushService(11677): <receiver androID:name="com.parse.ParsebroadcastReceiver">E/com.parse.PushService(11677):   <intent-filter>E/com.parse.PushService(11677):     <action androID:name="androID.intent.action.BOOT_COMPLETED" />E/com.parse.PushService(11677):     <action androID:name="androID.intent.action.USER_PRESENT" />E/com.parse.PushService(11677):   </intent-filter>E/com.parse.PushService(11677): </receiver>

编辑#2:

我更新了基于@L_404_0@处理gcm push的方式.在实现扩展’GcmListenerService’的类时,您现在可以轻松检查’from’参数是否与用于注册推送的Google项目ID相同.

public class MyGcmListenerService extends GcmListenerService {    @OverrIDe    public voID onMessageReceived(String from, Bundle data) {        // only handle gcm messages that come from the same project ID used to register        if (from.equals("Your Google project ID)) {            // handle the gcm push        }    }}

另外,解析更新了它们的库(我现在正在使用’1.9.4′)你可以继承’ParsePushbroadcastReceiver’来处理你想要的通知.有关基本实现,请参见指南here.

在’Application’类的’onCreate’方法中解析初始化:

Parse.initialize(this, "your parse app ID", "your parse clIEnt key");// subscribing to a channelParsePush.subscribeInBackground("your channel name", new SaveCallback() {    @OverrIDe    public voID done(ParseException e) {        if (e == null) {            Log.d("com.parse.push", "successfully subscribed to the broadcast channel.");        } else {            Log.e("com.parse.push", "Failed to subscribe for push");        }    }});

广播接收器实现:

public class MyParsePushbroadcastReceiver extends ParsePushbroadcastReceiver {    @OverrIDe    protected voID onPushReceive(Context context, Intent intent) {        // handle the parse push notification    }}

解析和gcm的清单声明:

...<!-- GCM Listener service --><service    androID:name=".MyGcmListenerService"    androID:exported="false" >    <intent-filter>        <action androID:name="com.Google.androID.c2dm.intent.RECEIVE" />    </intent-filter></service>...<!-- Parse broadcast receiver --><receiver androID:name=".MyParsePushbroadcastReceiver" androID:exported="false">    <intent-filter>        <action androID:name="com.parse.push.intent.RECEIVE" />        <action androID:name="com.parse.push.intent.DELETE" />        <action androID:name="com.parse.push.intent.OPEN" />    </intent-filter></receiver>...

我只添加了服务和接收器,但您需要确保遵循GCM指南和Parse推送指南以实现完整实现(例如,谷歌还添加了一种处理令牌刷新的方法 – 包含完整代码的示例样品可以找到here).

解决方法:

如果我理解正确,您希望您的代码处理所有传入的GCM消息,无论其来源(可以是您的服​​务器或Parse网站),这意味着您不希望应用程序中的Parse代码处理它们.

您可以通过在清单中仅声明处理com.Google.androID.c2dm.intent.RECEIVE *** 作的单个广播接收器来实现此目的.那将是你的GCMbroadcastReceiver类,它将处理所有到达的GCM消息.

当您声明处理相同 *** 作的两个广播接收器时,可能会发生您当前遇到的行为.

总结

以上是内存溢出为你收集整理的Android:如何使用Parse作为替代GCM推送通知提供程序?全部内容,希望文章能够帮你解决Android:如何使用Parse作为替代GCM推送通知提供程序?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1097768.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存