gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:21:13 24 4
gpt4 key购买 nike

请参阅问题末尾的编辑#2(Google 更新了推送的实现方式,因此处理 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”)类处理服务器注册和接收消息 - 它工作正常并且接收到所有推送消息。服务器有时会发送自定义数据,因此我自己处理消息并以编程方式创建通知(单击通知时使用的 Intent 有一些从服务器发送的重要附加信息)。

我想以某种方式使解析以相同的方式运行,以便能够从解析网站发送 channel 推送并创建我自己的通知,但我尝试的一切都失败了(遵循 android 推送教程并没有真正起作用对于我的问题)。有没有人尝试过类似的事情?在花了很多时间调整推送指南/教程后,我有点不知所措——有时我没有收到任何通知;有时 parse 和我的接收者都会被调用,我会收到双重通知。我还尝试使用解析 REST api 进行注册并自己处理所有事情,但发现这在 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 console
PushService.subscribe(context, MDConstants.PARSE_PUSH_CHANNEL, MainActivity.class);
ParseInstallation.getCurrentInstallation().saveInBackground();

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

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:

我更新了基于 google push notification developer guide 处理 gcm 推送的方式.在实现扩展“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
}
}
}

此外,parse 更新了它们的库(我现在使用的是“1.9.4”),您可以根据需要将“ParsePushBroadcastReceiver”子类化以处理通知。见指南here用于基本实现。

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

Parse.initialize(this, "your parse app id", "your parse client key");

// subscribing to a channel
ParsePush.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
}
}

parse 和 gcm 的 list 声明:

...

<!-- 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 推送指南以获得完整的实现(例如,谷歌还添加了一种处理 token 刷新的方法 - 示例可以找到包含完整代码示例的 here )。

最佳答案

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

您可以通过在 list 中仅声明一个广播接收器来处理 com.google.android.c2dm.intent.RECEIVE 操作来实现此目的。那将是您的 GCMBroadcastReceiver 类,它将处理所有到达的 GCM 消息。

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

关于Android:如何使用 Parse 作为替代 GCM 推送通知提供程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23055519/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com