각기 고유 한 데이터가 포함 된 여러 알림을 받아야하는 Android 애플리케이션을 개발 중입니다. 내가 세 가정 해 보겠습니다받은 :푸시 알림 클릭시 새 활동을 여는 방법은 무엇입니까?
상황이 이미 1. GCM 서비스를 데이터가 새로운 활동에
문제를 알림을 클릭에 서버에서 보내 2. 디스플레이를 사용하여 여러 알림을 수신 달성 한 알림에는 각각 고유 한 데이터가 있습니다. 하나의 알림을 클릭하면 해당 알림의 데이터로 새로운 활동이 시작됩니다. 이제 수신 통지 활동이 실행 중입니다. 이제 두 번째 알림을 클릭하면 알림 알림 활동이 이전 데이터 (첫 번째 알림의 알림)로로드됩니다. 응용 프로그램을 닫고 세 번째 알림을 클릭하면 세 번째 알림의 데이터와 함께 수신 알림 활동이로드됩니다.
나는 시도했다 : 어떤 성공
I없이 launchMode = "singleInstance":
- 는
- 안드로이드를 포함하는 매니페스트 파일을 편집 (작동하지 않는) 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()); } }
수신의 문제는 내 활동 조건을 실행에없는 내가 통지를받은 경우. 현재 알림에서 활동을 열면 새로운 데이터가 표시되지만 활동이 실행 중이고 동시에 알림이 도착하는 경우 나타납니다. 이 알림을 열면 이전 데이터로 활동이로드됩니다. 활동을 실행하고 있어도 새로운 데이터를 액티비티에 표시하고 싶습니다.
그래서 알림이 도착할 때마다 자동으로 활동을 열려고합니까? – Kedarnath
새로운 알림을 클릭하면 그 알림을 통해받은 데이터로 새로운 활동이 시작됩니다. 따라서 알림을 클릭하고 새 활동을 시작하는 것이 좋습니다. 내가 직면 한 문제는 이미 실행중인 활동으로 새 알림을 열려고 할 때입니다. 활동은 이전 알림의 이전 데이터 만 표시합니다. 응용 프로그램을 닫고 알림을 열면 제대로 작동합니다. – Durga
알 수 있듯이, 활동이 실행 중이 아니거나 이미 실행중인 경우에는 새로운 데이터를 표시하려는 경우 데이터를 표시하려고합니다. 맞습니까? – Kedarnath