2017-05-09 10 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からプッシュ通知を受信すると同時にアプリを開く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

はうまくいきませんでした..デバイスcantが主な問題である通知を受け取ることができると思います。 – Prashant

+0

@PrashantあなたのデバイスがGCM登録IDを取得しているかどうかを確認してください。 nullでなくサーバーに正常に保存されている場合は、サーバー側から通知が配信されているか、エラーがスローされているかどうかを確認します。 – Chowdary102

1

私はServiceクラスの内部でこのメソッドを使用しています:

@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; 
    } 

が許可を忘れないでください

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

(つまり、新しいAPIで廃止されましたが、それでも必要とされている)

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

+0

が動作していない....試してみました。デバイスが、この問題を通知することができないと思います。この@ Vyacheslavを削除するには – Prashant

関連する問題