2016-12-14 27 views
0

我是android新手。我正在學習android中的gcm集成部分,並且遇到了這個問題,我無法調用未在清單中標記爲啓動程序的活動。我能夠收到gcm通知和所有數據,但無法呼叫該活動。這是爲什麼發生?只有當應用程序從後臺移除時,我才面臨此問題。當應用程序沒有運行或從後臺移除時,是否有辦法存儲和檢索應用程序上下文?如何從GcmListenerService調用非啓動類?

public class NotificationsListenerService extends GcmListenerService { 

    public static final int MESSAGE_NOTIFICATION_ID = 435345; 
    private static final String TAG = "MyGcmListenerService"; 

    @Override 
    public void onMessageReceived(String from, Bundle data) { 
     //String message = data.getString("message"); 
     String message = data.getBundle("notification").getString("body"); 
     Log.d(TAG, "From: " + from); 
     Log.d(TAG, "Message: " + message); 
     Log.d(TAG, "GCM DATA: " + data); 

     try{ 
      String CODE=data.getString("code"); 
      if(CODE!=null){ 
       switch (CODE){ 
        case "666": 
         String id=data.getString("id"); 
         Code666(id); 
         break; 
       } 
      } 
     }catch (Exception e){ 
      e.printStackTrace(); 
     } 
    } 

    private void Code666(String _id) { 
     Context context = getBaseContext(); 
     Intent intent = new Intent(this, SecondActivity.class); 
     intent.putExtra("_id",_id); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
       PendingIntent.FLAG_ONE_SHOT); 
     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) 
       .setSmallIcon(R.drawable.floatinglogo) 
       .setContentTitle("Horn OK") 
       .setContentText("HelpNeeded!") 
       .setAutoCancel(true) 
       .setContentIntent(pendingIntent); 
     NotificationManager mNotificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build()); 
    } 
} 

在此先感謝! Sidharth

+0

嘗試用'flag_activity_new task'東西。 –

+0

發佈相關代碼。 –

+0

請發送您正在發送待定意圖並顯示通知的代碼。 –

回答

0

在意向

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

我使用這個方法添加這兩條線,它工作正常,HomeActivity是不是我的發射活動

private void sendNotification() { 
    Intent intent = new Intent(this, HomeActivity.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setContentTitle(getString(R.string.app_name)) 
      .setContentText(messageBody) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

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

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
} 
+0

是的,我試過這個。沒有解決這個問題。 –

+0

有沒有辦法在關閉應用程序時存儲應用程序上下文? –

+0

編輯我的答案,請檢查此作品適合我。 –

相關問題