Sie sollten eine Flagge definieren, welche Meldungsart auf Ihre Antwort vom Server enthält und entsprechend zu handeln. Hier ist ein Beispiel, wie man das erreicht:
public class MyGcmListenerService extends GcmListenerService
String message = "";
String image = "";
String user_id = "";
String name = "";
String time_received = "";
String notification_type = "";
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 jsonStr = data.getString("data");
Log.i(TAG,"JSON: "+jsonStr);
try {
JSONObject json = new JSONObject(jsonStr);
message = json.getString("message");
image = json.getJSONObject("user").getString("image");
name = json.getJSONObject("user").getString("name");
time_received = json.getJSONObject("user").getString("created_at");
user_id = json.getJSONObject("user").getString("user_id");
notification_type = json.getJSONObject("user").getString("notification_type");
} catch (JSONException e) {
e.printStackTrace();
}
Log.i(TAG, "Image: " + image);
Log.i(TAG, "From: " + name);
Log.i(TAG, "User ID: " + user_id);
Log.i(TAG, "Message: " + message);
Log.i(TAG, "Time Receievd: " + time_received);
sendNotification(message);
switch(notification_type)
{
case "Audio":
//Call your activity Here
break;
case "Video":
//Call your activity Here
break;
case "Message":
//Call your activity Here
break;
}
}
private void sendNotification(String message) {
Intent intent = new Intent(this, MainActivity.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_action)
.setContentTitle("Someone Commented on your Song.")
.setSubText("By "+name)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
Ich habe gerade bemerkt. Dies ist ziemlich genau die gleiche Idee wie Ihre Frage vor ein paar Tagen. –
Mögliche Duplikate von [behandeln verschiedene Arten von Benachrichtigungen in Android] (http://stackoverflow.com/questions/37065831/handle-different-types-of-notifications-in-android) –
@McAwesomville In dem ich versuchte zu bearbeiten mein Code aber konnte das nicht beenden endete mit dem Posten einer neuen Frage .. – SwagDevelopers