2017-05-09 49 views
0

我想打開應用程序,當設備從GCM接收推送通知時,同一時間的應用程序已經關閉,我試過這個代碼,它在一些設備上並不是在所有設備上都能正常工作。 下面是我的代碼片段,如何在應用程序關閉時收到推送通知(GCM)時在所有設備上打開android應用程序?

@Override 
public void onHandleIntent(Intent intent) { 
notificationManager(this, getString(R.string.new_ride), true); 

}

public static void notificationManager(Context context, String message, boolean 
ring) { 

     try { 
      long when = System.currentTimeMillis(); 

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

      Log.v("message",","+message); 

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


      notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
      PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 

      NotificationCompat.Builder builder = new NotificationCompat.Builder(context); 
      builder.setAutoCancel(true); 
      builder.setContentTitle(context.getString(R.string.app_name)); 
      builder.setStyle(new NotificationCompat.BigTextStyle().bigText(message)); 
      builder.setContentText(message); 
      builder.setTicker(message); 

      if(ring){ 
       builder.setLights(Color.GREEN, 500, 500); 
      } 
      else{ 
       builder.setDefaults(Notification.DEFAULT_ALL); 
      } 

      builder.setWhen(when); 
      builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher)); 
      builder.setSmallIcon(R.drawable.notif_icon); 
      builder.setContentIntent(intent); 
      Notification notification = builder.build(); 
      notificationManager.notify(NOTIFICATION_ID, notification); 
      //Open the App while new ride request arrives 
      Intent dialogIntent = new 
      Intent(getBaseContext(),SplashNewActivity.class);            
      dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);           
      getApplication().startActivity(dialogIntent); 
      } catch (Exception e) { 
          e.printStackTrace(); 
      } 

} 

請讓我知道是否有可能在所有設備上,或者是設備相關。 在此先感謝。

+0

「我想在設備從GCM同時接收推送通知時打開應用程序ap p已經關閉「 - 用戶可能不會欣賞你的活動,在他們正在做的事情中間打斷他們。用戶可能以多種方式表達他們的不滿(比如差評,身體暴力等)。 「我試過這個代碼,它可以在一些設備上運行,而不是在所有設備上運行」 - 請詳細解釋**「什麼是不是所有設備」的含義。當你使用你的調試器或日誌時,看看會發生什麼......會發生什麼? – CommonsWare

回答

0

嘗試報警管理器。希望這個作品!

 Intent resultIntent = new Intent(this, SplashNewActivity.class); 
     // This ensures that the back button follows the recommended 
     // convention for the back key. 
     TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 

     // Adds the back stack for the Intent (but not the Intent itself). 
     stackBuilder.addParentStack(SplashNewActivity.class); 

     // Adds the Intent that starts the Activity to the top of the stack. 
     stackBuilder.addNextIntent(resultIntent); 
     PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
       0, PendingIntent.FLAG_UPDATE_CURRENT); 

     long futureInMillis = System.currentTimeMillis(); 
     AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
     alarmManager.set(AlarmManager.RTC_WAKEUP, futureInMillis, resultPendingIntent); 

在您的onMessageReceived方法中調用此代碼。

+0

詳細說明您的答案:解釋這是做什麼,什麼行做什麼,並添加更多關於如何使用它 – Zoe

+0

嘗試不工作..我認爲設備無法接收通知這是主要問題。 – Prashant

+0

@Prashant FIrst確保您的設備正在獲取GCM註冊ID。如果它不爲空併成功保存在服務器中,請從服務器端檢查通知是否正在交付或發生錯誤。 – Chowdary102

1

我使用內部服務類此方法:

Intent myAppIntent = launchIntent(this); 
      startActivity(myAppIntent); 

其中

@SuppressLint("NewApi") 
    public static Intent launchIntent(Context ctx) { 
     final ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE); 
     Intent intent = new Intent(); 
     boolean activated = false; 


     if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      List<ActivityManager.AppTask> tasks = am.getAppTasks(); 

      for (ActivityManager.AppTask task: tasks){ 
       intent = task.getTaskInfo().baseIntent; 
       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       activated = true; 
       break; 
      } 
     } else { 
      @SuppressWarnings("deprecation") 
      final List<ActivityManager.RecentTaskInfo> recentTaskInfos = am.getRecentTasks(1024,0); 
      String myPkgNm = ctx.getPackageName(); 
      if (!recentTaskInfos.isEmpty()) { 
       ActivityManager.RecentTaskInfo recentTaskInfo; 
       final int size = recentTaskInfos.size(); 
       for (int i = 0; i < size; i++) { 
        recentTaskInfo = recentTaskInfos.get(i); 
        if (recentTaskInfo.baseIntent.getComponent().getPackageName().equals(myPkgNm)) { 
         intent = recentTaskInfo.baseIntent; 
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
         activated = true; 
        } 
       } 
      } 
     } 
     if (!activated) { 
      intent = new Intent(ctx, YourDefaultActivity.class); 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     } 

     return intent; 
    } 

不要忘了許可(即棄用新的API,但仍然需要)

<uses-permission android:name="android.permission.GET_TASKS" />

+0

不工作....試過。我認爲設備無法收到通知,這是主要問題。如何擺脫這@Vyacheslav – Prashant

相關問題