2016-05-06 55 views
1

我在android.Audio/Video調用和msg中有3種類型的通知。如何根據通知類型進行區分,並在不同通知中打開不同的活動。這是我應該點擊通知開啓活動。在android中處理不同類型的通知

private void sendNotification(long when,String msg) { 
    String notificationContent ="Notification Content Click Here to go more details"; 
    String notificationTitle ="This is Notification"; 
    //large icon for notification,normally use App icon 
    Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher); 
    int smalIcon =R.drawable.hcp; 
    String notificationData="This is data : "+msg; 

    /*create intent for show notification details when user clicks notification*/ 
    Intent intent =new Intent(getApplicationContext(), NotificationDetailsActivity.class); 
    intent.putExtra("DATA", notificationData); 

    /*create unique this intent from other intent using setData */ 
    intent.setData(Uri.parse("http://www.google.com")); 
    /*create new task for each notification with pending intent so we set Intent.FLAG_ACTIVITY_NEW_TASK */ 
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
    Log.d(TAG, "Preparing to send notification...: " + msg); 
    mNotificationManager = (NotificationManager) this 
      .getSystemService(Context.NOTIFICATION_SERVICE); 



    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
      this).setSmallIcon(R.drawable.hcp) 
      .setContentTitle("GCM Notification") 
      .setContentIntent(pendingIntent) 
      .setAutoCancel(true) 
      .setSmallIcon(smalIcon) 
      .setTicker(notificationTitle) 
      .setLargeIcon(largeIcon) 
      .setContentText(notificationContent)  
      .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) 
      .setContentIntent(pendingIntent); 
    ; 
    Notification notification=mBuilder.build(); 

    mBuilder.setContentIntent(pendingIntent); 
    mNotificationManager.notify((int) when, notification); 

    Log.d(TAG, "Notification sent successfully."); 
} 
+0

您可以在通知有效內容中添加標識符。您如何識別您的客戶端應用程序。 –

回答

0

假設您接收的數據(msg)來自Web服務。現在與msg參數一起,您必須獲得一些其他參數,通過這些參數您可以區分以下通知是音頻/視頻呼叫還是消息。

所以,你可以創建一個將返回它可以用來代替NotificationDetailsActivity.class

private Class<?> getClassFromType(String type) { 
    if(type.equals("Audio") 
     return AudioActivity.class; 
    else if(type.equals("Video") 
     return VideoActivity.class; 
    else if(type.equals("Message") 
     return MessageActivity.class; 
} 

類名的最終代碼會是這樣的一個方法:

Intent intent =new Intent(getApplicationContext(), getClassFromType(type)); 

這裏type會區分通知的變量。

希望這會解決您的問題。

相關問題