接收安卓通知
Posted
技术标签:
【中文标题】接收安卓通知【英文标题】:Receive Android notification 【发布时间】:2014-03-12 20:22:33 【问题描述】:我已关注this 文档以便能够接收 GCM 通知。 这是我的清单:
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="18" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<permission
android:name="com.example.gcm.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_android_api_key" />
<activity
android:name="com.evapp.activities.SplashActivity"
android:label="@string/title_activity_splash" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.evapp.activities.LoginActivity"
android:label="@string/title_activity_login" >
</activity>
<activity
android:name="com.evapp.activities.RegistrationActivity"
android:label="@string/title_activity_signup"
android:parentActivityName="com.evapp.activities.LoginActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.evapp.activities.LoginActivity" />
</activity>
<activity
android:name="com.evapp.activities.HomeActivity"
android:label="@string/title_activity_home" >
</activity>
<activity
android:name="com.evapp.activities.UserActivity"
android:label="@string/title_activity_user" >
</activity>
<activity
android:name="com.evapp.activities.TestActivity"
android:label="@string/title_activity_test" >
</activity>
<receiver
android:name="com.evapp.notification.Test"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.evapp.notification" />
</intent-filter>
</receiver>
<service android:name="com.evapp.activities.GcmIntentService" />
</application>
这里是WakefulBroadcastReceiver
:
public class Test extends WakefulBroadcastReceiver
@Override
public void onReceive(Context context, Intent intent)
Intent gcmIntent = new Intent(context, GcmIntentService.class);
gcmIntent.putExtras(intent.getExtras());
startWakefulService(context, gcmIntent);
setResultCode(Activity.RESULT_OK);
还有IntentService
:
public class GcmIntentService extends IntentService
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public GcmIntentService()
super("GcmIntentService");
@Override
protected void onHandleIntent(Intent intent)
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty())
// has effect of unparcelling Bundle
/*
* Filter messages based on message type. Since it is likely that
* GCM will be extended in the future with new message types, just
* ignore any message types you're not interested in, or that you
* don't recognize.
*/
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
.equals(messageType))
sendNotification("Send error: " + extras.toString());
else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType))
sendNotification("Deleted messages on server: "
+ extras.toString());
// If it's a regular GCM message, do some work.
else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
.equals(messageType))
// This loop represents the service doing some work.
for (int i = 0; i < 5; i++)
try
Thread.sleep(5000);
catch (InterruptedException e)
// Post notification of received message.
sendNotification("Received: " + extras.toString());
// Release the wake lock provided by the WakefulBroadcastReceiver.
Test.completeWakefulIntent(intent);
// Put the message into a notification and post it.
// This is just one simple example of what you might choose to do with
// a GCM message.
private void sendNotification(String msg)
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, HomeActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.calendar)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
从我的服务器端我收到了这个响应:
"multicast_id":7269872883857375111,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1394655370108962%e210e99800364492"]
但是在调试时我永远不会进入GcmIntentService
,调试器正在进入Test
类并且没有任何反应。
我错过了什么?看来startWakefulService
打不开GcmIntentService
。
注意: 我已经按照@Eran 的建议更改了代码(仍然无法正常工作:()
【问题讨论】:
【参考方案1】:ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
这仅在您的 GCMIntentService 类位于应用程序的主包中时才有效,我认为情况并非如此(因为您的接收器类位于不同的包中 - com.evapp.notification
- 与应用程序的主包不同 - com.evapp.activities
)。
你可以试试这样的:
Intent gcmIntent = new Intent(context, GcmIntentService.class);
gcmIntent.putExtras(intent.getExtras());
startWakefulService(context, gcmIntent);
【讨论】:
仍然没有,我是否也应该更改清单?这是gcmIntent
(tosString()) Intent cmp=com.evapp.activities/com.evapp.notification.GcmIntentService (has extras)
的值
@VladIoffe 如果您发布完整清单,我将能够回答这个问题。
@VladIoffe 应用的包名是com.evapp.activities
。该软件包名称应替换您拥有 com.example.gcm
的位置。
@VladIoffe 也应该替换<category android:name="com.evapp.notification" />
中的包名。以上是关于接收安卓通知的主要内容,如果未能解决你的问题,请参考以下文章