我即将为我的应用程序实现 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的Service怎么知道要检查什么权限呢?据我了解(如果我在这里错了,请纠正我),在声明权限时,我可以在我的命名空间中选择任何权限名称,例如 com.example.myapp.permission.WHATEVER。
还是 C2D_MESSAGE 是我必须使用的一些魔法常数?
此外,文档说我必须为com.google.android.c2dm.intent.C2D_MESSAGE
和com.google.android.c2dm.intent.REGISTRATION
意图实现接收器。但是在代码示例中,接收者的过滤器只包含.intent.RECEIVE
和.intent.REGISTRATION
Intents。去哪儿了C2D_MESSAGE
,和我上面的问题有关系吗?
我希望这不是显而易见的事情,但我就是不明白......请帮忙。