我正在处理PUSH Notification。我在从GCM接收消息时使用以下代码:
public class MyGcmListenerService extends GcmListenerService {
private static final String TAG = "MyGcmListenerService";
/**
* Called when message is received.
*
* @param from SenderID of the sender.
* @param data Data bundle containing message data as key/value pairs.
* For Set of keys use data.keySet().
*/
// [START receive_message]
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
Log.e(TAG, "From: " + from);
Log.e(TAG, "Message: " + message);
if (from.startsWith("/topics/")) {
// message received from some topic.
} else {
// normal downstream message.
}
// [START_EXCLUDE]
/**
* Production applications would usually process the message here.
* Eg: - Syncing with server.
* - Store message in local database.
* - Update UI.
*/
/**
* In some cases it may be useful to show a notification indicating to the user
* that a message was received.
*/
sendNotification(message);
// [END_EXCLUDE]
}
// [END receive_message]
/**
* Create and show a simple notification containing the received GCM message.
*
* @param message GCM message received.
*/
private void sendNotification(String message) {
Intent intent = new Intent(this, WelcomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
//.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle("GCM Message")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notificationBuilder.build());
}
}
收到消息但未显示为通知。我不知道我在这里缺少什么。请帮我解决问题。
日志:
03-07 16:50:21.617 32412-32678/com.almabay.almachat E/MyGcmListenerService﹕ From: 20130254292
03-07 16:50:21.618 32412-32678/com.almabay.almachat E/MyGcmListenerService﹕ Message: Hi Deepak this is is notification from GCM server. 07.03.2016 16:20:52
答案 0 :(得分:1)
public void notify(int id,Notification notification)
发布要在状态栏中显示的通知。如果您的应用程序已经发布了具有相同ID的通知但尚未取消,则该通知将被更新的信息替换。
namespace :api, defaults: {format: :json} do
namespace :v1 do
...
match "/status", to: "application#status", via: "get"
...
end
end
希望这会让你有机会了解通知如何在android中运行。