在android中处理不同类型的通知

时间:2016-05-06 06:35:11

标签: android notifications google-cloud-messaging

我在android.Audio/Video call和msg中有3种类型的通知。如何根据通知类型进行区分,并在不同的通知上打开不同的活动。这是我点击通知时打开的活动。

 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.");
}

1 个答案:

答案 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将是区分通知的变量。

希望这能解决您的问题。