【问题标题】:How to handle gcm notification onReceive behaviour depends on notification type如何处理 gcm 通知 onReceive 行为取决于通知类型
【发布时间】:2016-05-10 05:44:38
【问题描述】:

我从 GCM.Audio/Video 通话和 msg 收到 3 种类型的 android 通知。如何在不同的通知上打开不同的活动。这就是我在点击通知时打开活动的方式。 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);


}

【问题讨论】:

  • 我刚刚注意到。这与您几天前的问题几乎相同。
  • @McAwesomville 我试图编辑我的代码但无法这样做,最后发布了一个新问题..
  • 对不起。你能详细说明吗?据我了解,您试图修改帖子中的代码?
  • @McAwesomville 是的..但它总是显示错误所以必须发布新问题..你有什么解决方案

标签: android push-notification google-cloud-messaging broadcastreceiver


【解决方案1】:

您应该在来自服务器的响应中定义一个包含通知类型的标志,并采取相应的措施。 以下是如何实现这一目标的示例:

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());
}
}

【讨论】:

  • 这里我没有发送任何消息,它由我的应用程序服务器处理。我只是在处理它。这里放置 GCMBroadcastReceiver 类。
  • 如果我使用 IntentService 类的 onHandleIntent 方法,如何从服务器获取数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-13
  • 1970-01-01
  • 1970-01-01
  • 2017-08-02
相关资源
最近更新 更多