C2DM:如何使用C2D_MESSAGE权限?

发布于 2024-10-19 09:35:47 字数 1448 浏览 1 评论 0原文

我即将为我的应用程序实现 C2DM,但我发现文档有点令人困惑关于如何编写清单。

清单代码示例包含以下内容:

<!-- Only this application can receive the messages and registration result --> 
<permission android:name="com.example.myapp.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.example.myapp.permission.C2D_MESSAGE" />

解释如下:

applicationPackage + ".permission.C2D_MESSAGE 阻止其他应用程序注册和接收该应用程序的消息。

但这到底是如何工作的?据我了解,这声明了一个权限,然后为我的应用程序获取该权限。但是该权限到底在哪里强制执行呢?

给出的注册代码是:

Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); // boilerplate
registrationIntent.putExtra("sender", emailOfSender);
startService(registrationIntent);

接收registrationIntent的服务如何知道要检查什么权限?据我了解(如果我在这里错了,请纠正我),在声明权限时,我可以选择我的权限中的任何权限名称。命名空间,例如 com.example.myapp.permission.WHATEVER。

或者 C2D_MESSAGE 是我必须使用的魔法常量吗?


另外,文档说我必须实现 com.google.android.c2dm.intent 的接收器。 C2D_MESSAGE 和 com.google.android.c2dm.intent.REGISTRATION Intent。但是在代码示例中,接收器的过滤器仅包含 .intent.RECEIVE 和 < code>.intent.REGISTRATION 意图。 C2D_MESSAGE 去了哪里,它与我上面的问题有关系吗?

我希望这不是显而易见的事情,但我只是不明白......请帮助。

I am about to implement C2DM for my application, but I find the documentation a bit confusing regarding how to write the manifest.

The manifest code example contains this:

<!-- Only this application can receive the messages and registration result --> 
<permission android:name="com.example.myapp.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.example.myapp.permission.C2D_MESSAGE" />

This is explained as follows:

applicationPackage + ".permission.C2D_MESSAGE prevents other applications from registering and receiving the application's messages.

But how exactly does this work? As I understand, this declares a permission and then gets that permission for my app. But where exactly is that permission enforced?

The code given for registration is:

Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); // boilerplate
registrationIntent.putExtra("sender", emailOfSender);
startService(registrationIntent);

How can the Service that receives the registrationIntent know what permission to check for? As I understand (and correct me if I am wrong here), when declaring the permission, I could have chosen any permission name within my namespace, e.g. com.example.myapp.permission.WHATEVER.

Or is C2D_MESSAGE some magic constant that I have to use?


Also, the documentation says that I have to implement receivers for com.google.android.c2dm.intent.C2D_MESSAGE and com.google.android.c2dm.intent.REGISTRATION Intents. But In the code example, the receiver's filters only contain .intent.RECEIVE and .intent.REGISTRATION Intents. Where did C2D_MESSAGE go, and does it have something todo with my the question above?

I hope this is not something obvious, but I just don't get it... please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜心 2024-10-26 09:35:47

他们在摘要顶部声称您需要的接收器似乎是文档中的错误,因为它与清单示例不匹配。

“com.google.android.c2dm.intent.C2D_MESSAGE 和 com.google.android.c2dm.intent.REGISTRATION 的接收器。”)

我得到了他们提供的示例,只需使用经过调整以匹配我的特定应用程序包的示例清单条目即可工作姓名。

当 Android 中的 C2DM 代码发送广播时(或者更确切地说,当它寻找将广播发送到的潜在接收者时),它会查找:

查找 Intent 的广播接收器:com.google.android.c2dm.intent.REGISTRATION
具有权限:.permission.C2D_MESSAGE

Android C2DM 代码发送到您的应用程序的所有 C2DM 消息都将寻找该特定权限。它特定于您的应用程序,并且只需要位于该结构中(您的应用程序的包+“.permission.C2D_MESSAGE”)。

关于注册请求如何获取用于权限的包名称,这是在您首次使用“app”额外注册时处理的 - 来自文档:

“app是应用程序的ID,使用PendingIntent设置以允许注册服务提取应用程序信息。”

有问题的行:

registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); // boilerplate

The receivers they claim you need at the top in the summary appears to be a mistake in the documentation, as it does not match the manifest example.

"Receivers for com.google.android.c2dm.intent.C2D_MESSAGE and com.google.android.c2dm.intent.REGISTRATION.")

I got the examples they give to work using simply the example manifest entries tweaked to match my particular application's package name.

When the C2DM code in Android sends the broadcast out (or, rather, when it looks for potential receivers to send the broadcast to), it looks for:

Find broadcast receivers for Intent: com.google.android.c2dm.intent.REGISTRATION
That have the permission: .permission.C2D_MESSAGE

All C2DM messages sent to your application by the Android C2DM code will be looking for that particular permission. It's specific to your application, and simply needs to be in that structure (your application's package + ".permission.C2D_MESSAGE").

Regarding how the registration request gets the package name to use for the permissions, that is handled when you first register with the "app" extra - from the documentation:

"app is the application's ID, set with a PendingIntent to allow the registration service to extract application information."

the line in question:

registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); // boilerplate
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文