在Android上推送通知 [英] Push notification on Android
问题描述
我创建一个要在其上实现推送通知的应用程序.由于有许多教程,我已经创建了服务器部分和客户端部分.
I create an application on which I want to implement Push Notifications. Thanks to many tutorials I've create the server part and the client part.
在服务器端,我发送了一个带有cURL的POST请求(在PHP中).服务器响应: {"multicast_id":5560733296047502303,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1408610711700930936%f027011f9fd7ecd"}]}
On the server part, I send a POST request with cURL (in PHP).
The server responds: {"multicast_id":5560733296047502303,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1408610711700937%6f027011f9fd7ecd"}]}
但是我没有在手机上收到通知.
But I don't receive the notification on my phone.
在应用程序中,我有一项服务:Manifest.xml
In the application I have a service: Manifest.xml
<service android:name=".GCMIntentService" />
以及所有必需的权限.
GCMIntentService.java
GCMIntentService.java
protected void onMessage(Context context, Intent intent) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
//notification.sound = Uri.parse("android.resource://"+ context.getPackageName()+ "your_sound_file_name.mp3");
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
我该如何调试?还是有明显的原因?
How can I debug this? Or is there any obvious reason?
推荐答案
在设备上,您需要执行以下步骤:
On Device, you need to perform following steps :
-
注册到GoogleCloudMessaging.
Register to GoogleCloudMessaging.
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);字符串gcmRegistrationId = gcm.register();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context); String gcmRegistrationId = gcm.register();
将步骤1中获得的gcmRegistrationId发送到服务器.服务器将使用此ID发送GCM挠痒痒.
Send gcmRegistrationId got in step 1 to Server. Server will use this id to send GCM tickle.
注册GCM接收器.如下所示将其添加到AndroidManifest.xml中:
Register GCM Receiver. Add it in AndroidManifest.xml as below :
<receiver
android:name="com.hp.msa.receiver.GCMReceiver"
android:exported="true"
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" />
</intent-filter>
</receiver>
GCMReceiver.java将如下所示:
GCMReceiver.java will look as below :
public class GCMReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Here, you will get actual PUSH notification.
// After receiving it, you can perform your tasks
// Intent contains data sent by server
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
// Logic
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
// Logic
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// Logic
}
}
}
这篇关于在Android上推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!