0

我使用推送通知显示在我的应用程序片段显示片段,但我的实现看起来不稳定从推送通知

有应用程序运行时两种情况,当应用程序没有运行

在这两种情况下我希望应用程序显示特定片段

这是我FragmentActivity在我的清单文件

<activity 
     android:name=".Main" 
     android:label="@string/app_name" 
     android:screenOrientation="portrait" 
     android:theme="@style/Theme.NoBackground" 
     android:windowSoftInputMode="adjustPan" > 
    </activity> 

这里为m Ÿ代码当它收到一个GCM消息

private void sendNotification(String message) { 

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

    int requestID = (int) System.currentTimeMillis(); 

    final PendingIntent contentIntent = PendingIntent.getActivity(
      getApplicationContext(), requestID, new Intent(
        getApplicationContext(), Main.class).setAction(Actions.SHOW_FRIENDS_FRAGMENT), PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
      this) 
      .setSmallIcon(R.drawable.notification_icon) 
      .setAutoCancel(true) 
      .setContentText(message) 
      .setSound(
        RingtoneManager 
          .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); 

    mBuilder.setContentIntent(contentIntent); 
    mNotificationManager.notify(Util.getApp().getNotificationId(), mBuilder.build()); 
} 

一个在我的活动创建通知,这是由服务,称为我在这两个的onCreate和onNewIntent

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    checkIntent(intent); 
} 

@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 
    checkIntent(intent); 
} 

private void checkIntent(Intent i){ 
    try{ 
     String action = i.getAction(); 

     if(action != null){ 
      if(action.equalsIgnoreCase(Actions.SHOW_FRIENDS_FRAGMENT)){ 
       showFriendsFragment(); 
      } 
     } 
    }catch(Exception e){ 

    } 
} 

检查的问题是,如果动作在应用程序重新启动时在onCreate中找到,动作仍然存在,因此它始终在该页面上启动。 在onNewIntent中找到该操作时不会发生该行为,但仅在该应用程序正在运行时才会调用该行为。

我试过使用临时演员,并显示片段后,删除他们更改挂起意图标志CANCEL_CURRENT,ONE_SHOT但行为仍然是相同的。

回答

2

因此经过几天的努力找出这一点。我一直无法找到任何传统的方法来解决这个问题,所以我使用了黑客技术。

  1. 在您的主要活动定义public static boolean checkIntent = false
  2. 创建被称为一个虚拟的活动,当你在DummyActivity的通知点击所以

    final PendingIntent contentIntent = PendingIntent.getActivity(
        getApplicationContext(), requestID, new Intent(
          getApplicationContext(), DummyActivity.class).putExtra("data", data), PendingIntent.FLAG_UPDATE_CURRENT); 
    
  3. @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    
    Intent i = getIntent(); 
    if (i != null) { 
        String data = i.getStringExtra("data"); 
        Main.checkIntent = true; 
        startActivity(new Intent(getBaseContext(), Main.class).putExtra(
          "data", data)); 
    } 
    finish(); 
    } 
    
  4. 最后在checkIntent方法中

    private void checkIntent(Intent i){ 
    try{ 
    String data = i.getStringExtra("data"); 
    
    if(data!= null){ 
        checkIntent = false; 
        showFriendsFragment(); 
    } 
    }catch(Exception e){ 
    
    } 
    }