Android GCM 自行创建通知
Android GCM Creating Notifications on its own
我设置了 GCM,它工作正常。我从服务器收到通知并将它们显示给客户端。问题是,GCM 会创建自己的通知,不会让我自定义通知。我遵循了这个指南:https://github.com/codepath/android_guides/wiki/Google-Cloud-Messaging。
这是我的服务代码:
public class GcmMessageHandler extends GcmListenerService {
public static final int MESSAGE_NOTIFICATION_ID = 435345;
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
createNotification(from, message);
}
private void createNotification(String title, String body) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true);
Intent resultIntent = new Intent(this, RouteActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(RouteActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build());
}
}
任何帮助。
Android 中通知的自定义布局,如果这就是您的意思:Create custom notification, android
来自文档
Message payload is optional. If you are including a payload in the message, use the data parameter to include your custom key/value
pairs. The client app handles the data payload for display or other
processing purposes.
The notification parameter with predefined options indicates that GCM
will display the message on the client app’s behalf if the client app
implements GCMListenerService on Android, or whenever the notification
message is sent to an iOS device. The app server can send a message
including both notification and data payloads. In such cases, GCM
handles displaying the notification payload and the client app handles
the data payload. For more information and examples, see
换句话说,如果您在有效负载中发送一个 notification
标签,GCM 将为您的应用程序创建一个通知
我设置了 GCM,它工作正常。我从服务器收到通知并将它们显示给客户端。问题是,GCM 会创建自己的通知,不会让我自定义通知。我遵循了这个指南:https://github.com/codepath/android_guides/wiki/Google-Cloud-Messaging。
这是我的服务代码:
public class GcmMessageHandler extends GcmListenerService {
public static final int MESSAGE_NOTIFICATION_ID = 435345;
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
createNotification(from, message);
}
private void createNotification(String title, String body) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true);
Intent resultIntent = new Intent(this, RouteActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(RouteActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build());
}
}
任何帮助。
Android 中通知的自定义布局,如果这就是您的意思:Create custom notification, android
来自文档
Message payload is optional. If you are including a payload in the message, use the data parameter to include your custom key/value pairs. The client app handles the data payload for display or other processing purposes.
The notification parameter with predefined options indicates that GCM will display the message on the client app’s behalf if the client app implements GCMListenerService on Android, or whenever the notification message is sent to an iOS device. The app server can send a message including both notification and data payloads. In such cases, GCM handles displaying the notification payload and the client app handles the data payload. For more information and examples, see
换句话说,如果您在有效负载中发送一个 notification
标签,GCM 将为您的应用程序创建一个通知