C2DM:如何使用C2D_MESSAGE权限?
我即将为我的应用程序实现 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
他们在摘要顶部声称您需要的接收器似乎是文档中的错误,因为它与清单示例不匹配。
我得到了他们提供的示例,只需使用经过调整以匹配我的特定应用程序包的示例清单条目即可工作姓名。
当 Android 中的 C2DM 代码发送广播时(或者更确切地说,当它寻找将广播发送到的潜在接收者时),它会查找:
Android C2DM 代码发送到您的应用程序的所有 C2DM 消息都将寻找该特定权限。它特定于您的应用程序,并且只需要位于该结构中(您的应用程序的包+“.permission.C2D_MESSAGE”)。
关于注册请求如何获取用于权限的包名称,这是在您首次使用“app”额外注册时处理的 - 来自文档:
有问题的行:
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.
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:
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:
the line in question: