我在从GCM.Audio/Video调用和消息收到的android中有3种类型的通知。如何在不同的通知上打开不同的活动。这就是我点击通知时打开活动的方式。 GcmBroadcastReceiver.java
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(),
GCMNotificationIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
答案 0 :(得分:0)
您应该定义一个标志,其中包含来自服务器的响应的通知类型,并相应地执行操作。 以下是如何实现这一目标的示例:
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());
}
}