问题
I received the response from the server and I parsed that response in the android now I have to set the Notification Title and Text from that response. I means from that particular function.
Here I am putting my ParseList() function here where i parsed my response.
private void parseList(Bundle bundle) {
String id,name,photo,updates;
try {
JSONObject json = new JSONObject();
Set<String> keys = bundle.keySet();
for (String key : keys) {
try {
json.put(key, bundle.get(key));
} catch (JSONException e) {
e.printStackTrace();
}
}
JSONArray jsonarray = new JSONArray(json.getString("response"));
Log.e("","Hi"+jsonarray);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonObjectmain = jsonarray .getJSONObject(i);
JSONArray array = jsonObjectmain.getJSONArray("updates");
for (int j = 0; j < array.length(); j++) {
JSONObject jsonObject = array.getJSONObject(j);
id = jsonObject.getString("id").toString();
name = jsonObject.getString("name").toString();
photo = jsonObject.getString("photo").toString();
updates = jsonObject.getString("updates").toString();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
There is a parameter called name,updates and photo. Now i have to setup the Notification Title from this response How can i set up that thing.. ?
I have onMessage() method with me but how can i put that values into that. ??
Here is my onMessage() method for Notification...
@Override
protected void onMessage(Context context, Intent data) {
String message;
message = data.getExtras().getString("name");
Intent intent = new Intent(this, MyActivity.class);
parseList(data.getExtras());
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setContentTitle(message)
.setContentText(message).setContentIntent(pIntent)
.getNotification();
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(R.string.app_name, notification);
{
PowerManager pm = (PowerManager) context
.getSystemService(Context.POWER_SERVICE);
final PowerManager.WakeLock mWakelock = pm.newWakeLock(
PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP, "GCM_PUSH");
mWakelock.acquire();
notification.defaults|=Notification.DEFAULT_VIBRATE;
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
mWakelock.release();
}
};
timer.schedule(task, 5000);
}
}
回答1:
first your notification message must contains the values in different keys.
array( "title" => $title, "message" => $message )
the above code is php server side sample array data which is actually being added to 'data' key and then the data is sent to applications using the different servers. I have used GCM and while receiving data from server message get the above key data from bundle.
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setContentTitle(title)
.setContentText(message).setContentIntent(pIntent)
.getNotification();
here you can check that setContentTitle is used to add "title" and setContentText is the "subTitle" or message
回答2:
You should use NotificationCompat.Builder to set the title, icon, content etc. for a notification. Here's an example:
private NotificationCompat.Builder mBuilder;
mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.whitelogo)
.setLargeIcon(anImage)
.setContentTitle(title); //set smallIcon,largeIcon, contentTitle for your notification.
and to add the notification :
Notification.Builder notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setContentTitle(message)
.setContentText(message)
.setContentIntent(pIntent);
notificationManager.notify(NOTIFICATION_ID_NOTIF, notification.build());
UPDATE
It's not working in your case because, there's no String with key name in your bundle, so this will return null :
message = data.getExtras().getString("name");
so move your notification code in the parseList method, as there you have already extracted all the values (id,name,photo,updates).
来源:https://stackoverflow.com/questions/38158673/how-to-setup-setcontenttitle-and-setcontenttext-for-push-notification-from-json