21

我試圖在點擊通知時打開Activity,下面是我的代碼。點擊通知沒有打開提到的活動

Intent intent = new Intent(this.getApplicationContext(), NotificationActivity.class); 
intent.putExtra("msgBody",messageBody); 
intent.putExtra(Constants.NOTIF_INTENT_TYPE,Constants.NOTIF_INTENT_TYPE); 

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
       |Intent.FLAG_ACTIVITY_SINGLE_TOP 
       |Intent.FLAG_ACTIVITY_CLEAR_TOP); //Tried with many options here 

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent, 
             PendingIntent.FLAG_CANCEL_CURRENT); 

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.otp_icon) 
       .setContentTitle("Push MSG") 
       .setContentText(messageBody) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

NotificationManager notificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

notificationManager.notify(0, notificationBuilder.build()); 

Android清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.com.pushapp"> 

    <uses-sdk 
     android:minSdkVersion="17" 
     android:targetSdkVersion="21" /> 

    <supports-screens 
     android:anyDensity="true" 
     android:largeScreens="true" 
     android:normalScreens="true" 
     android:smallScreens="true" /> 

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.READ_LOGS" /> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

    <application 
     android:name=".AndroidPushApp" 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher"> 
     <activity 
      android:name=".PushSplashScreen" 
      android:screenOrientation="portrait"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".MainApplicationScreen" 
      android:screenOrientation="portrait" 
      android:windowSoftInputMode="adjustResize"> 
      <intent-filter> 
       <action android:name="android.intent.action.VIEW" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
       <category android:name="android.intent.category.BROWSABLE" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".StartActivity" 
      android:launchMode="singleTask" 
      android:screenOrientation="portrait" 
      android:uiOptions="splitActionBarWhenNarrow" 
      android:windowSoftInputMode="adjustResize"> 
      <intent-filter> 
       <action android:name="android.intent.action.VIEW" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
       <category android:name="android.intent.category.BROWSABLE" /> 
      </intent-filter> 
     </activity> 

     <service android:name=".MyFirebaseMessagingService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.MESSAGING_EVENT" /> 
      </intent-filter> 
     </service> 

     <service android:name=".MyFirebaseInstanceIDService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> 
      </intent-filter> 
     </service> 

     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 

     <activity 
      android:name=".NotificationActivity" 
      android:exported="true" 
      android:label="@string/title_activity"> 
      <intent-filter> 
       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

每當我從FCM得到通知我打電話此通知。 NotificationActivity沒有打開,只要我點擊通知,而是應用程序打開(我平常的應用程序流的splash screen->starting activity)。每當我在應用程序已經打開的時候收到通知,NotificationActivity就會被打開,但是當應用程序尚未打開時就不會。有人能幫我解決這個問題嗎?

注:請我重申NotificationActivity.class上,當應用程序尚未打開狀態的通知點擊時是沒有得到開放。

+0

使用FLAG_ONE_SHOT https://開頭開發商.android.com/reference/android/app/PendingIntent.html#FLAG_ONE_SHOT並讓我知道。 –

+0

@SagarGangawane試過沒有運氣 – rick

+0

@rick請發佈您的_AndroidManifest.xml_。 – earthw0rmjim

回答

0

您可以指定任何Activity成爲推送通知接收器:

<intent-filter> 
    <action android:name="PACKAGE_NAME.MESSAGE"/> 
    <category android:name="android.intent.category.DEFAULT"/> 
</intent-filter> 

這爲活動意圖過濾指定哪些活動將在響應中推出的推送通知(PACKAGE_NAME是你的Android應用程序包)

因此,您可以在您要打開的Activity中添加此意圖過濾器,只需點擊推送通知即可。

+0

爲什麼選擇投票?它爲我工作。也請嘗試添加此行。 Adheesh

0

如果您詳細閱讀火力文檔,有兩種類型的有效載荷

  • 數據有效載荷
  • 通知有效載荷

的數據有效載荷觸發onMessageReceived( )當app是前景和背景時回調。通知有效負載不是這種情況,它只在前臺狀態下觸發回調。所以,如果你使用數據有效載荷,應該解決這個問題。

+0

通知在我的情況下每次都會被解僱。我在打開屏幕時遇到問題,而不是通知觸發 – rick

+1

嘗試在意圖中使用FLAG_ACTIVITY_CLEAR_TOP標誌,在PendingIntent中使用FLAG_CANCEL_CURRENT標誌。 –

0

檢查此代碼並讓我知道。

 Intent intent = new Intent(this, LoginActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     pendingIntent = PendingIntent.getActivity(this, 0, intent, 
       PendingIntent.FLAG_ONE_SHOT); 
0

您必須在pendingIntent中使用FLAG_UPDATE_CURRENT

PendingIntent pendingIntent = PendingIntent.getActivity(this, notificationId /* Request code */, intent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 

,並通過同一個ID notificationManager

notificationManager.notify(notificationId /* ID of notification */, notificationBuilder.build()); 
+0

感謝您的回答。我已經嘗試過,但沒有運氣。 – rick

0

多數民衆贊成在intented行爲。如果你的應用程序在後臺,通知是由android系統創建的,它沒有你的pendingIntent動作。所以它不起作用。在前臺情況下,它的工作原理是通知是由您的代碼創建的。

請檢查下面的鏈接文檔。 https://firebase.google.com/docs/notifications/android/console-device#receive_and_handle_messages

+0

然後,如何通過點擊通知來打開預期的活動,而不管應用程序是處於前景還是後臺。現在,當我的應用程序處於後臺時,它會在點擊通知時打開它。當服務器通過gcm或fcm向設備發送通知有效載荷時,請建議 – rick

+0

,您必須添加數據有效載荷,即「data」:{「your_action_key」:「action_value_like_activity」}並從您的_onMessageRecievedmethod(RemoteMessage remoteMessage)_中讀取並創建通知通過解析remoteMessage.getData()。 – rafa

14

FCM文檔,用於接收和處理消息,

如果你想收到通知時,您的應用程序是在 前景,你需要添加一些消息處理邏輯。

要接收消息,請使用延伸到 FirebaseMessagingService的服務。您的服務應覆蓋 onMessageReceived回調,該回調爲大多數消息類型提供, ,但以下情況除外:

1)。當您的應用處於後臺時發送的通知。在 這種情況下,通知被傳遞到設備的系統托盤。 默認情況下,用戶點擊通知將打開應用程序啓動器。 2)。包含通知和數據有效負載的郵件,包括背景 和前景。在這種情況下,通知將傳送到 設備的系統托盤,並且數據有效負載將以您的啓動程序活動意圖的附加組件 傳送。

enter image description here

因此,基本上,我們有兩種類型的有效載荷

1)的。通知有效負載

2)。數據有效載荷

3)。兩者(我們可以考慮的另一種類型)。

現在讓我們逐個討論這些有效載荷。在此之前,您需要了解如何將這些有效負載發送到您的應用程序。您只需使用任何可執行HTTP POST Request的工具即可。在我的情況下,我使用郵差工具,一個谷歌瀏覽器插件。

之前作出HTTP Post RequestFCM,你必須要考慮三件事情:

1)。 HTTP發佈請求網址:https://fcm.googleapis.com/fcm/send

2)。請求頭:

i)。內容類型:應用程序/ json

ii)。授權:key = YOUR_SERVER_KEY

以下是顯示其外觀的截圖。

enter image description here

3)。正文:在這我們將有JSONNotificationData Payloads

  • 因此,從開始通知有效負載,最簡單的。在這種情況下,onMessageReceived()被稱爲只有當應用程序是在Foreground,對於所有其他情況下,這是一個System Tray Notification,點擊它時打開Launcher Activity。如果您不想自己控制Notifications,並且在Notification到來時沒有太多數據需要處理,這會很有幫助。您甚至可以在沒有在您的onMessageReceived()中編寫任何代碼的情況下控制聲音,圖標和click_action(僅在應用程序位於Foreground時)。在下面的屏幕截圖中附加了HTTP POST Request這個主體的一個示例。

enter image description here

如果要打開發送click_action參數時所需Activity,你必須使用下面的代碼在你的onMessageReceived()

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    if (null != remoteMessage.getNotification().getClickAction()) { 
      startActivity(remoteMessage.getNotification().getClickAction(), null, this); 
    } 
} 

以下是您startActivity()方法:

public void startActivity(String className, Bundle extras, Context context) { 
    Class cls = null; 
    try { 
     cls = Class.forName(className); 
    } catch (ClassNotFoundException e) { 
     //means you made a wrong input in firebase console 
    } 
    Intent intent = new Intent(context, cls); 
    if (null != extras) { 
     intent.putExtras(extras); 
    } 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
      | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
    context.startActivity(intent); 
} 

注:此click_action鍵將只有當應用程序在 前景,對於所有其他情況下,當應用程序在後臺和 已關閉,它不起作用。如果指定了此參數,它甚至不會在「背景」和「關閉」情況下打開啓動器活動, 。

  • 現在到了數據有效載荷。這與我們在GCM中的那個類似。這是非常重要的,如果我們想要處理Notification所有的東西,我們都在做GCM的情況。下面顯示了HTTP POST Request的一個主體的示例。

enter image description here

因此,在這種情況下,onMessageReceived()被稱爲每次,這將在一樣的方法的GCM工作,這樣有助於我們所有人。您必須如下所示OverrideonMessageReceived()

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    Map<String, String> data = remoteMessage.getData(); 
    if (null != data && 0 < data.size()) { 
     if (data.containsKey("custom_key_1")) { 
      sendNotification(data.get("custom_key_1")); 
     } 
    } 
} 

private void sendNotification(String messageBody) { 
     Intent intent = new Intent(this, DesiredActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
       PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.ic_stat_ic_notification) 
       .setContentTitle("FCM Message") 
       .setContentText(messageBody) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

     NotificationManager notificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
} 
  • 最後但並非最不重要的,我們可以同時發送NotificationData Payloads爲好。在這種情況下,當應用程序位於Foreground時,將調用onMessageReceived()。對於背景和關閉狀態,Notification進入系統托盤類似於Notification Payload,但唯一的區別是我們可以有data extras以及我們可以用來將用戶重定向到期望的Activity,當點擊Notification時。以下是這種HTTP POST Request的主體的示例。下面示出了這樣的HTTP POST Request的主體的示例。

enter image description here

當在系統托盤上的Notification點擊,它會打開Launcher Activity,你需要OverrideonCreate()Launcher Activity的得到data extras並重定向用戶到所需Activity。以下是代碼,您必須編寫您的ActivityonCreate()以將用戶重定向到所需的Activity

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if(getIntent().hasExtra("custom_key_1") && getIntent().getStringExtra("custom_key_1") 
      .equals("custom_value_1")){ 
     startActivity(new Intent(this, DesiredActivity.class)); 
     finish(); 
     return; 
    } 

    // other operations 
} 

另一種情況,以這種類型是,當你的Launcher Activitymanifest被定義爲launchMode="true"並且當Notification到達時,你的Launcher Activity處於Foreground。因此,當您點擊通知時,您必須在Launcher ActivityOverrideonNewIntent()方法中打開所需的Activity。以下是相同的示例代碼。

@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 
    if (getIntent().hasExtra("custom_key_1") && getIntent().getStringExtra("custom_key_1") 
       .equals("custom_value_1")) { 
      startActivity(new Intent(this, DesiredActivity.class)); 
      finish(); 
    } 
} 

因此,在短期,我會說這是好去與數據有效載荷類型,因爲它提供了更多的靈活性和控制權Notification而我們都被用來GCM更重要的,所以這種類型我們都想要更喜歡什麼。

注意:有些設備在 中收到通知的問題背景,因爲我在這裏發現了一些相同的查詢。此外,在 時間,我正在調查這些情況下,我的華碩手機沒有在上述任何類型的背景中收到 通知。所以 不知道這些設備有什麼問題。

+0

如上所述,我需要打開通知點擊活動,而不是onmessagereceived,請建議 – rick

+0

該解決方案處理每個案件相同。請再次通過它,讓我知道你沒有得到 –

+0

請看看更新的答案,希望這將有助於:) –

0

對不起,沒有評論,因爲我是一個相當新的人。

你可以做進一步調查以下兩兩件事:通知已創建

  1. 後,使用shell命令「亞行外殼dumpsys活動我[你的包名]」中看到您的通知,細節,以確認它真的是你想要的。 請記住用您自己的軟件包名稱替換「[您的軟件包名稱]」;

  2. 在使用「adb logcat -v threadtime -b events」重現此事件期間跟蹤事件日誌。

發佈這兩個,我們可能會得到一些有關什麼是錯誤的問題。

0

設置如下圖所示

Intent intent = new Intent(this.getApplicationContext(), NotificationActivity.class); 
intent.putExtra("msgBody",messageBody); 
intent.putExtra(Constants.NOTIF_INTENT_TYPE,Constants.NOTIF_INTENT_TYPE); 

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); 

它添加到使用

.setContentIntent(pendingIntent); 
0

我在FirebaseMessagingService使用您的通知尚待處理的意圖:

/** 
* Create and show a simple notification containing the received FCM message. 
* 
* @param messageBody FCM message body received. 
*/ 
private void sendNotification(String title, String messageBody, String data) { 
    Intent intent = new Intent(this, MainActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
      PendingIntent.FLAG_ONE_SHOT); 

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext()) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentTitle(title) 
      .setContentText(messageBody) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

    NotificationManager notificationManager = 
      (NotificationManager) getSystemService(this.NOTIFICATION_SERVICE); 

    notificationManager.notify(id++ /* ID of notification */, notificationBuilder.build()); 
} 
相關問題