Android活动没有收到来自GCM的消息

时间:2014-05-05 22:08:23

标签: android android-fragments google-cloud-messaging splash-screen flags

我的GCM客户端有问题。 我收到了通知,我已经阅读了该消息但我无法调用“TestView”活动。

这是GCMIntentService.java:

@Override protected void onMessage(Context context, Intent intent1) {
    String message = intent1.getExtras().getString("message");
    Log.i(TAG, "new message= "+message);
    int icon = R.drawable.icon;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);
    String title = context.getString(R.string.app_name);
    Intent notificationIntent = null;
    notificationIntent = new Intent(context, TestView.class);
    notificationIntent.putExtra("msg", message);
    //notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);
}

这是manifest.xml:

 <application
            android:allowBackup="true"
            android:icon="@drawable/icon"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="example.SplashScreen"
                android:label="@string/title_activity_splash_screen"
                android:noHistory="true"
                android:screenOrientation="portrait"
                android:theme="@style/Theme.AppCompat.Light" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name="example.TestView"
                android:label="@string/app_name"
                android:theme="@style/Theme.AppCompat.Light" >
            </activity>
            <meta-data
                android:name="com.google.android.gms.version"
                android:value="@integer/google_play_services_version" />

这是SplahScreen.java:

public class SplashScreen extends ActionBarActivity {
    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen); 
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
            //everything is ok, works!
        Bundle extras = getIntent().getExtras();
        String msg = extras.getString("msg");
        Log.d("MSG","msg:"+msg); 
        ...
        new Handler().postDelayed(new Runnable() {   
            @Override  
            public void run() {  
                Intent intent = new Intent(SplashScreen.this,  TestView.class);  
                startActivity(intent);  
                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);  
            }  }, 3);   
        }

这是TestView.java:

public class TestView extends ActionBarActivity implements ... {
    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        //doesn't work
        Bundle extras = getIntent().getExtras();
        String msg = extras.getString("msg");
        Log.d("MSG","msg:"+msg); 

如果我更换:

notificationIntent = new Intent(context, TestView.class);

使用:

notificationIntent = new Intent(context, SplashScreen.class);

一切都好!

我想将“message”从“GCMIntentService”发送到“TestView”。

感谢您在这种情况下提供的任何帮助

1 个答案:

答案 0 :(得分:1)

在TestView活动中尝试此操作:

  public void onNewIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        if (extras != null) { ...