2014-03-05 5 views
5

각기 고유 한 데이터가 포함 된 여러 알림을 받아야하는 Android 애플리케이션을 개발 중입니다. 내가 세 가정 해 보겠습니다받은 :푸시 알림 클릭시 새 활동을 여는 방법은 무엇입니까?

상황이 이미 1. GCM 서비스를 데이터가 새로운 활동에

문제를 알림을 클릭에 서버에서 보내 2. 디스플레이를 사용하여 여러 알림을 수신 달성 한 알림에는 각각 고유 한 데이터가 있습니다. 하나의 알림을 클릭하면 해당 알림의 데이터로 새로운 활동이 시작됩니다. 이제 수신 통지 활동이 실행 중입니다. 이제 두 번째 알림을 클릭하면 알림 알림 활동이 이전 데이터 (첫 번째 알림의 알림)로로드됩니다. 응용 프로그램을 닫고 세 번째 알림을 클릭하면 세 번째 알림의 데이터와 함께 수신 알림 활동이로드됩니다.

나는 시도했다 : 어떤 성공

I없이 launchMode = "singleInstance":

  1. 안드로이드를 포함하는 매니페스트 파일을 편집 (작동하지 않는) FLAG_ACTIVITY_CLEAR_TOP하는 의도 플래그 설정 사용 중임

    resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    

    intentservice에서매니페스트 파일의이 작동하지 않습니다.

    intentservice 클래스는

    public class GcmIntentService extends IntentService{ 
    
    Context context; 
    public static int notify_no=0; 
    
    //System.currentTimeMillis(); 
    
    private NotificationManager mNotificationManager; 
    NotificationCompat.Builder builder; 
    public static final String TAG = "GCM NOTIFICATION"; 
    
    public GcmIntentService() { 
        super("GcmIntentService"); 
        // TODO Auto-generated constructor stub 
    } 
    
    @Override 
    protected void onHandleIntent(Intent intent) { 
        // TODO Auto-generated method stub 
        Bundle extras = intent.getExtras(); 
        String msg = intent.getStringExtra("message"); 
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); 
        String messageType = gcm.getMessageType(intent); 
    
    
        if (!extras.isEmpty()) { 
    
         if (GoogleCloudMessaging. 
            MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { 
           // sendNotification(RegIdDTO.REG_ID,"Send error: " + extras.toString()); 
          sendNotification(this,msg); 
          } else if (GoogleCloudMessaging. 
            MESSAGE_TYPE_DELETED.equals(messageType)) { 
           // sendNotification(RegIdDTO.REG_ID,"Deleted messages on server: " + 
           //   extras.toString()); 
           sendNotification(this,msg); 
          // If it's a regular GCM message, do some work. 
          } else if (GoogleCloudMessaging. 
            MESSAGE_TYPE_MESSAGE.equals(messageType)) { 
           // This loop represents the service doing some work. 
           for (int i=0; i<5; i++) { 
            Log.i(TAG, "Working... " + (i+1) 
              + "/5 @ " + SystemClock.elapsedRealtime()); 
            try { 
             Thread.sleep(500); 
            } catch (InterruptedException e) { 
            } 
           } 
           Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime()); 
           // Post notification of received message. 
           //sendNotification("Received: " + extras.toString()); 
           // sendNotification(RegIdDTO.REG_ID,msg); 
           sendNotification(this,msg); 
           Log.i(TAG, "Received: " + extras.toString()); 
          } 
         } 
        GcmBroadcastReceiver.completeWakefulIntent(intent); 
    } 
    
    
    private static void sendNotification(Context context,String message) { 
        int icon = R.drawable.ic_stat_gcm; 
        long when = System.currentTimeMillis(); 
        NotificationCompat.Builder nBuilder; 
        Uri alarmSound = RingtoneManager 
          .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
        nBuilder = new NotificationCompat.Builder(context) 
          .setSmallIcon(R.drawable.ic_launcher) 
          .setContentTitle("header") 
          .setLights(Color.BLUE, 500, 500).setContentText(message) 
          .setAutoCancel(true).setTicker("Notification from Traffic") 
          .setVibrate(new long[] { 100, 250, 100, 250, 100, 250 }) 
          .setSound(alarmSound) 
          ; 
    
        String consumerid = null; 
        Integer position = null; 
          // write your click event here 
         Intent resultIntent = new Intent(context, NotificationReceiveActivity.class); 
    
         resultIntent.putExtra("message", message); 
         // resultIntent.setData(Uri.parse("content://"+when)); 
         resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    
    PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 
         notify_no, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
         // Show the max number of notifications here 
        if (notify_no < 9) { 
        notify_no = notify_no + 1; 
        } else { 
        notify_no = 0; 
        } 
    nBuilder.setContentIntent(resultPendingIntent); 
    NotificationManager nNotifyMgr = (NotificationManager) context 
         .getSystemService(context.NOTIFICATION_SERVICE); 
        nNotifyMgr.notify(notify_no + 2, nBuilder.build()); 
        } 
    
    } 
    

    내가 알림을받을 수 있어요 위의 코드를 사용하여 활동을

    public class NotificationReceiveActivity extends Activity { 
    
        TextView name; 
        TextView deal; 
        TextView valid; 
        TextView address; 
        JSONObject json; 
        GcmIntentService serv; 
        Context mContext; 
        // static boolean active = false; 
    
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_notification_receive); 
    
    
        Intent intent = getIntent(); 
    
        name = (TextView) findViewById(R.id.name); 
        deal = (TextView) findViewById(R.id.deal); 
        valid = (TextView) findViewById(R.id.valid); 
        address = (TextView)findViewById(R.id.address); 
        String message = intent.getExtras().getString("message"); 
    
        try { 
         json = new JSONObject(message); 
         String stime = json.getString("name"); 
         name.setText(stime); 
    
         String slecturename = json.getString("deal"); 
         deal.setText(slecturename); 
    
         String sroom = json.getString("valid"); 
         valid.setText(sroom); 
    
         String sfaculty = json.getString("address"); 
         address.setText(sfaculty); 
    
    
        } catch (JSONException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
    } 
    
    @Override 
    protected void onResume() { 
        // TODO Auto-generated method stub 
        super.onResume(); 
        serv=new GcmIntentService(); 
        //serv.CancelNotification(getApplicationContext()); 
    
    } 
    } 
    

    수신의 문제는 내 활동 조건을 실행에없는 내가 통지를받은 경우. 현재 알림에서 활동을 열면 새로운 데이터가 표시되지만 활동이 실행 중이고 동시에 알림이 도착하는 경우 나타납니다. 이 알림을 열면 이전 데이터로 활동이로드됩니다. 활동을 실행하고 있어도 새로운 데이터를 액티비티에 표시하고 싶습니다.

+0

그래서 알림이 도착할 때마다 자동으로 활동을 열려고합니까? – Kedarnath

+0

새로운 알림을 클릭하면 그 알림을 통해받은 데이터로 새로운 활동이 시작됩니다. 따라서 알림을 클릭하고 새 활동을 시작하는 것이 좋습니다. 내가 직면 한 문제는 이미 실행중인 활동으로 새 알림을 열려고 할 때입니다. 활동은 이전 알림의 이전 데이터 만 표시합니다. 응용 프로그램을 닫고 알림을 열면 제대로 작동합니다. – Durga

+0

알 수 있듯이, 활동이 실행 중이 아니거나 이미 실행중인 경우에는 새로운 데이터를 표시하려는 경우 데이터를 표시하려고합니다. 맞습니까? – Kedarnath

답변

2

새로운 의도를 얻었을 때 활동이 singleTop이거나 의도가 FLAG_ACTIVITY_SINGLE_TOP 인 경우 활동의 onNewIntent에 새로운 의도가 실제로 전달됩니다. 문서를 인용 :

이 그들의 패키지에 launchMode에 "singleTop"을 설정하거나 (의도) startActivity를 호출 할 때 클라이언트가 FLAG_ACTIVITY_SINGLE_TOP 플래그를 사용한 경우 활동이라고합니다. 두 경우 모두 시작되는 활동의 새 인스턴스 대신 활동 스택의 맨 위에있는 동안 활동이 다시 시작되면 on-newIntent()가 다시 시작하는 데 사용 된 인 텐트가있는 기존 인스턴스에서 호출됩니다 그것.

새로운 의도를 받기 전에 활동이 일시 중지되므로이 메소드 후에 호출되는 onResume()을 기대할 수 있습니다.

getIntent()는 여전히 원래 의도를 반환합니다. setIntent (Intent)를 사용하여이 새로운 Intent로 업데이트 할 수 있습니다.

NotificationReceiveActivity에서 해당 방법을 재정의하면 문제가 해결되어야합니다.

+0

내 코드를 수정 해 주실 수 있습니까? – Durga

0

귀하의 활동에서 onNewIntent을 무시하십시오.

@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 
    intent.getExtra("message", message); 
} 
관련 문제