从GCM数据开始不同的活动

时间:2015-07-02 16:11:14

标签: android google-cloud-messaging

我有一个接收GCM通知的应用程序,并根据收到的数据启动某些活动。我有几个活动与通知中获得的值具有相同的名称。匹配时,通过单击通知启动特定活动。这些是我从通知中获得的值:msg1和msg2。类似的东西:

if (msg1=="John") {
  // fires activity John ... }
if (msg1=="Mark") {
  // fires activity Mark ... }

这是我的GCMIntenceService.java:

public class GcmIntentService extends IntentService {

    public static final int NOTIFICATION_ID = 1;
    private static final String TAG = "GcmIntentService";
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    Context context;
    public static final int notifyID = 9001;
    String msg1;
    String msg2;

    public GcmIntentService() {
        super("GcmIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        String msg1 = intent.getStringExtra("cat_name");
        String msg2 = intent.getStringExtra("message");
     //   String msg = intent.getStringExtra("message");
        String msg = msg1 + ":" + msg2;
        // The getMessageType() intent parameter must be the intent you received
        // in your BroadcastReceiver.
        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {  // has effect of unparcelling Bundle
            /*
             * Filter messages based on message type. Since it is likely that GCM
             * will be extended in the future with new message types, just ignore
             * any message types you're not interested in, or that you don't
             * recognize.
             */
            if (GoogleCloudMessaging.
                    MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                sendNotification("Send error: " + extras.toString());
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_DELETED.equals(messageType)) {
                sendNotification("Deleted messages on server: " +
                        extras.toString());
            // If it's a regular GCM message, do some work.
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                // This loop represents the service doing some work.
                for (int i=0; i<5; i++) {
                    Log.i(TAG, "Working... " + (i+1)
                            + "/5 @ " + SystemClock.elapsedRealtime());
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                    }
                }
                Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                // Post notification of received message.
            //    sendNotification(extras.getString("Notice"));
                sendNotification(msg);
                Log.i(TAG, "Received: " + extras.toString());
            }
        }
        // Release the wake lock provided by the WakefulBroadcastReceiver.
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }

    private void sendNotification(String msg) {

        if (msg1 == "John") {
            Intent resultIntent = new Intent(this, Katarina.class);
            resultIntent.putExtra("message", msg);
            PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,
                    resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        }

        if (msg1 == "Mark") {
            Intent resultIntent = new Intent(this, Andrija.class);
            resultIntent.putExtra("message", msg);
            PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,
                    resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        }





        NotificationCompat.Builder mNotifyBuilder;
        NotificationManager mNotificationManager;

        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        mNotifyBuilder = new NotificationCompat.Builder(this)
           //     .setContentTitle("Alert")
          //      .setContentText("You've received new message.")
                .setContentTitle("")
                .setContentText(msg)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                         .setSmallIcon(R.drawable.gcm_cloud);
        // Set pending intent
        mNotifyBuilder.setContentIntent(resultPendingIntent);

        // Set Vibrate, Sound and Light
        int defaults = 0;
        defaults = defaults | Notification.DEFAULT_LIGHTS;
        defaults = defaults | Notification.DEFAULT_VIBRATE;
        defaults = defaults | Notification.DEFAULT_SOUND;

        mNotifyBuilder.setDefaults(defaults);
        // Set the content for Notification
    //    mNotifyBuilder.setContentText("New message from Server");
        // Set autocancel
        mNotifyBuilder.setAutoCancel(true);
        // Post a notification
        mNotificationManager.notify(notifyID, mNotifyBuilder.build());
    }


    }

但我收到错误CannotResolve resultPendingIntent排队: mNotifyBuilder.setContentIntent(resultPendingIntent)

谢谢!

1 个答案:

答案 0 :(得分:0)

resultPendingIntentif块内定义,因此在离开这些块时超出范围。将resultPendingIntent设为整个sendNotification()方法的局部变量。