2012-10-19 3 views
0

좋아, 여기 내 문제가있다. 푸시 알림을 수신하는 Service이 있습니다. 이제 푸시 알림의 내용에 따라 해당 애플리케이션을 클릭해야 시작됩니다.서비스에서 응용 프로그램 시작

푸시 알림을 수신하거나 서비스를 사용하는 데 아무런 문제가 없습니다. 작동하지 못하는 유일한 방법은 올바른 애플리케이션을 여는 부분입니다.

Service에서 신청서를 열려면 무엇을해야합니까? 이 서비스와 함께 작동하는 여러 개의 앱이 있기 때문에 이것은 동적으로 이루어져야합니다.

누군가 올바른 방향으로 나를 가리킬 수 있다면 좋을 것입니다.

추신. 도서관에 보관하는 GCM 서비스를 사용하고 있으므로 여러 앱에서 사용할 수 있습니다. 이것은 내가 URL의 내용을 확인하고 알림에 대한 올바른 의도를 설정해야하는 GCMIntentService.onMessage() 기능입니다 :

@Override 
protected void onMessage(Context arg0, Intent arg1) 
{ 
    Log.i(TAG, "new message = " + arg1.getExtras()); 

    //Get a reference to the NotificationManager 
    String ns = Context.NOTIFICATION_SERVICE; 
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 

    //Instantiate the Notification  
    int icon = getResourseIdByName(arg0.getApplicationContext().getPackageName(), "drawable", "notification_icon"); 
    CharSequence tickerText = arg1.getExtras().getString("tickertext"); 
    long when = System.currentTimeMillis(); 

    Notification notification = new Notification(icon, tickerText, when); 

    //Define the notification's message and PendingIntent 
    Context context = getApplicationContext(); 
    CharSequence contentTitle = arg1.getExtras().getString("contentTitle"); 
    CharSequence contentText = arg1.getExtras().getString("contentText"); 

    Intent notificationIntent = null; 

    if(arg1.getExtras().getString("url").isEmpty()) 
    { 
     notificationIntent = new Intent(this, arg1.getExtras().getString("packageName").getClass()); 
    } 
    else 
    { 
     notificationIntent = new Intent(this, MyWebView.class); 
    } 

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK); 

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
    notification.flags = Notification.FLAG_AUTO_CANCEL; 

    //Pass the Notification to the NotificationManager 
    int notificationId = Integer.parseInt(arg1.getExtras().getString("notificationId")); 

    mNotificationManager.notify(notificationId, notification); 
} 

답변

2

응용 프로그램이 Intent의를 사용하여 시작할 수 있습니다. 그냥 빨리보기에서,

Intent startingIntent = context.getPackageManager().getLaunchIntentForPackage("target_app_package_name"); 
context.startActivity(startingIntent); 
+0

시도했지만 작동하지 않습니다. – DijkeMark

+0

지금 사용합니다 : notificationIntent = arg0 – DijkeMark

1

아니라 : 당신이 코드를 사용할 수 있습니다,

Intent targetIntent = new Intent(Intent.ACTION_MAIN, null); 
targetIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
ComponentName cn = new ComponentName("target_app_package_name", "target_activity_class_name"); 
targetIntent.setComponent(cn); 
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, targetIntent, Intent.FLAG_ACTIVITY_NEW_TASK); 

당신이 목표 패키지를 모르거나 Activity을 대상으로하지 않은 경우 :이 같은 올바른 PendingIntent 객체를 생성 할 필요가 그것에서, 당신은 해당 응용 프로그램의 전체 패키지 이름을 얻을 수있을 것 같습니다.

는 다음 방금 패키지 이름으로 응용 프로그램이

Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.your.packageName.in.String"); 
startActivity(LaunchIntent); 

편집과 같이 시작할 수 있습니다 : 그 내용은

을, AFAIK, 당신은 단지 그 통지를 만들어 자신의 패키지에서 활동을 시작할 수 있습니다. 다른 응용 프로그램 패키지에서 활동을 시작할 수는 있지만 일반적으로 알 수없는 해당 패키지의 활동 클래스 이름을 알아야합니다.

Intent clickIntent = new Intent(context,Activity.class); 
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,clickIntent,0); 

알림을 만들 때 필요한 기능입니다.

notification.setlatestEventInfo(context, "title","content",pendingIntent); 

urgh! 엉터리 주석 죄송합니다 ... 코멘트에 코드를 넣을 수 없습니다. (

+0

그러나 이것을 푸시 알림에 넣을 수 있습니까? – DijkeMark

+0

편집을 확인하십시오. :) –

관련 문제