2016-12-14 4 views
0

私はアンドロイドを新しくしています。私はアンドロイドで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); 

に、私はこの方法を使用していますし、それが正常に動作し、これらの2行を追加し、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

私の答えを編集してくださいこれは私のために適切にチェックしてください。 –

関連する問題