2016-05-06 17 views
1

Ich habe 3 Arten von Benachrichtigungen in android.Audio/Video Anruf und msg. So differenzieren Sie basierend auf dem Benachrichtigungstyp und öffnen Sie verschiedene Aktivitäten für verschiedene Benachrichtigungen. So aktiviere ich die Aktivität beim Klicken auf die Benachrichtigung.behandeln verschiedene Arten von Benachrichtigungen in 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

Sie können eine Kennung in Ihrer Benachrichtigungsnutzlast hinzufügen. Wie du das identifizierst ist innerhalb deiner Client App. –

Antwort

0

Angenommen, dass die Daten (msg) von einem Webdienst empfangen werden. Nun müssen Sie zusammen mit dem Parameter msg einige weitere Parameter erhalten, mit denen Sie unterscheiden können, ob die folgende Benachrichtigung ein Audio/Video-Anruf oder eine Nachricht ist.

So können Sie eine Methode erstellen, die den Namen der Klasse zurück, die anstelle von verwendet werden kann 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; 
} 

Der endgültige Code wird so etwas wie dieses:

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

Hier type wird die Variable, die die Benachrichtigung unterscheidet.

Ich hoffe, dies wird Ihr Problem lösen.

Verwandte Themen