2016-12-14 47 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

编辑我的答案,请检查此作品适合我。 –