2015-04-27 28 views
1

我嘗試從我設置的應用程序服務器接收到的通知中打開活動的時間非常糟糕。我有一個Android服務,當用戶登錄到處理接收通知的應用程序服務器時運行。當用戶點擊通知時,應該啓動一個名爲「通知」的活動。在Android服務的相關代碼:從服務不能正常工作發出的通知中打開活動

int requestID = (int) System.currentTimeMillis(); 

Intent intent = new Intent(); 
intent.setComponent(new ComponentName(this, Notifications.class));  
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
//intent.putExtra("json", message); 
PendingIntent pintent = PendingIntent.getActivity(this, requestID, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

然後,設置標題和文本的通知後:

NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.plus) 
      .setContentTitle(title) 
      .setContentText(caption) 
      .setContentIntent(pintent) 
      .setLights(Color.BLUE, 1000, 1000) 
      .setVibrate(new long[] {0, 100, 100, 100}); 

NotificationManager mNotifyMgr = 
     (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

mNotifyMgr.notify(100, mBuilder.build()); 

在整體上可Notifications活動:

package com.myapp; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 

public class Notifications extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_notifications); 
    } 
} 

我宣佈我在清單文件中的活動:

<activity 
    android:name=".Notifications" 
    android:label="@string/title_activity_notifications" 
    android:screenOrientation="portrait" 
    android:launchMode="singleTask" 
    android:taskAffinity="" 
    android:excludeFromRecents="true"> 
</activity> 

我很困惑的是,這個特定的活動不會打開 - 我試圖設置它來打開其他活動,它的工作原理很好。這是我從logcat中時的精確時刻我點擊通知:

04-26 23:43:33.301: D/StatusBar(896): Clicked on content of 0|com.myapp|100|null|10107 
04-26 23:43:33.312: I/ActivityManager(725): START u0 {flg=0x10008000 cmp=com.myapp/com.google.android.gms.games.Notifications (has extras)} from uid 10107 on display 0 
04-26 23:43:33.315: D/audio_hw_primary(189): out_set_parameters: enter: usecase(1: low-latency-playback) kvpairs: routing=2 
04-26 23:43:33.322: W/InputMethodManagerService(725): Window already focused, ignoring focus gain of: [email protected] attribute=null, token = [email protected] 
04-26 23:43:33.326: D/audio_hw_primary(189): select_devices: out_snd_device(2: speaker) in_snd_device(0: none) 
04-26 23:43:33.326: D/msm8974_platform(189): platform_send_audio_calibration: sending audio calibration for snd_device(2) acdb_id(15) 
04-26 23:43:33.326: D/audio_hw_primary(189): enable_snd_device: snd_device(2: speaker) 
04-26 23:43:33.330: D/audio_hw_primary(189): enable_audio_route: apply and update mixer path: low-latency-playback 
04-26 23:43:33.703: D/PhoneStatusBar(896): disable: < expand icons* alerts system_info* back home recent clock search > 

是否有事情做與在logcat中看到的com.google.android.gms.games.Notifications文本?我在Nexus 5

編輯

所以運行的是Android 5.1,我改名活動來Notification,現在一切都很好。我沒有一個合適的解釋,爲什麼它首先失敗,所以我會留下這個問題,直到/如果有人可以充分解釋發生在第一位,因爲我不打算現在花更多時間在它上面。

回答

0

這很清楚發生了什麼事。你有這樣的代碼來設置通知Intent

intent.setComponent(new ComponentName(this, Notifications.class)); 

您必須導入包com.google.android.gms.games這樣編譯器使用,而不是com.google.android.gms.games.Notifications自己的類的。當您更改班級的名稱時,編譯器能夠選擇正確的班級。看看你的import聲明。