Android GCM 通知:无标题
Android GCM notification : no title
我正在构建一个 Android 应用程序,目前正在处理通知。我正在使用 Google 云消息服务,为此我编写了以下 PHP 代码:
<?php
define('API_ACCESS_KEY', 'blablabla');
$registrationIds = array($this->deviceId);
$data = array
(
'title' => 'My Title',
'message' => 'My message',
'subtitle' => 'This is a subtitle. subtitle',
'tickerText' => 'Ticker text here...Ticker text here...',
'vibrate' => 1,
'sound' => 1
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $data
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
curl_exec($ch);
curl_close($ch);
当我执行此 php 文件时,我会在我的 Android 设备上收到通知,如您在以下屏幕截图中所见。消息文本没问题。
android screenshot
但问题是通知的标题不是我在负载中写的 'My Title',而是 'GCM Message'(看起来像默认的 GCM 标题)。
尝试了很多不同的东西,但没有弄明白。
有什么我错的想法吗?谢谢
-------- 解决方案--------
我完全忘记了这必须在 android 端处理...解决方案是在 MyGcmListenerService 中自定义 sendNotification() 方法来处理额外数据:
private void sendNotification(String title, String message) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
感谢解答!
为了设置自定义通知标题,您应该在 GcmListenerService 的 onMessageReceived(String form, Bundle data) 方法中创建自己的通知 class...我为创建自定义通知所做的工作在以下代码:
public class MyGcmListenerService extends GcmListenerService {
private PendingIntent resultPendingIntent;
@Override
public void onMessageReceived(String from, Bundle data) {
NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.app_logo)
.setContentTitle(data.getString(getResources().getString(R.string.notification_title)))
.setContentText(data.getString(getResources().getString(R.string.notification_body)))
.setDefaults(Notification.DEFAULT_VIBRATE)
.setContentIntent(resultPendingIntent);
int mNotificationId = 001;
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, nBuilder.build());
}
}
在 setContentTitle 中设置自定义通知标题的位置。
我正在构建一个 Android 应用程序,目前正在处理通知。我正在使用 Google 云消息服务,为此我编写了以下 PHP 代码:
<?php
define('API_ACCESS_KEY', 'blablabla');
$registrationIds = array($this->deviceId);
$data = array
(
'title' => 'My Title',
'message' => 'My message',
'subtitle' => 'This is a subtitle. subtitle',
'tickerText' => 'Ticker text here...Ticker text here...',
'vibrate' => 1,
'sound' => 1
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $data
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
curl_exec($ch);
curl_close($ch);
当我执行此 php 文件时,我会在我的 Android 设备上收到通知,如您在以下屏幕截图中所见。消息文本没问题。 android screenshot 但问题是通知的标题不是我在负载中写的 'My Title',而是 'GCM Message'(看起来像默认的 GCM 标题)。 尝试了很多不同的东西,但没有弄明白。
有什么我错的想法吗?谢谢
-------- 解决方案--------
我完全忘记了这必须在 android 端处理...解决方案是在 MyGcmListenerService 中自定义 sendNotification() 方法来处理额外数据:
private void sendNotification(String title, String message) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
感谢解答!
为了设置自定义通知标题,您应该在 GcmListenerService 的 onMessageReceived(String form, Bundle data) 方法中创建自己的通知 class...我为创建自定义通知所做的工作在以下代码:
public class MyGcmListenerService extends GcmListenerService {
private PendingIntent resultPendingIntent;
@Override
public void onMessageReceived(String from, Bundle data) {
NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.app_logo)
.setContentTitle(data.getString(getResources().getString(R.string.notification_title)))
.setContentText(data.getString(getResources().getString(R.string.notification_body)))
.setDefaults(Notification.DEFAULT_VIBRATE)
.setContentIntent(resultPendingIntent);
int mNotificationId = 001;
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, nBuilder.build());
}
}
在 setContentTitle 中设置自定义通知标题的位置。