小编Soh*_*uri的帖子

通过通知将变量传递给活动

这是我的代码:

GCMIntentService:

Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.putExtra("screen", activityToShow);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent intent2 = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder nBuilder = 
        new NotificationCompat.Builder(this)
        .setSmallIcon(smallIconToDisplayInGCM)
        .setLargeIcon(largeIconToDisplayInGCM)
        .setContentTitle(title)
        .setContentText(text)
        .setSound(sound)
        .setTicker(ticker)
        .setContentIntent(intent2)
        .setAutoCancel(true);
Run Code Online (Sandbox Code Playgroud)

主要活动:

的onCreate()

onNewIntent(getIntent()) 
Run Code Online (Sandbox Code Playgroud)

onCreate()之外的新方法

@Override
public void onNewIntent(Intent intent){
    Bundle extras = intent.getExtras();
    String tabNumber = "";

    if(extras != null) {
        tabNumber = extras.getString("screen");
        Log.d("TEMP", "Tab Number: " + tabNumber);
        if(tabNumber.equals("") || (tabNumber == null)) {
            // Set the active tab to be the Home …
Run Code Online (Sandbox Code Playgroud)

notifications android

4
推荐指数
1
解决办法
6925
查看次数

标签 统计

android ×1

notifications ×1