简体   繁体   中英

handle different types of notifications in android

I have 3 types of notifications in android.Audio/Video call and msg. How to differentiate based on notification type and open different activity on different notification. THis is hoW I open activity on click of notification.

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

Assuming that the data (msg) your receiving is from a web service. Now along with the msg parameter, you must be getting some other parameters by which you can distinguish that the following notification is Audio/Video call or message.

So you can create a method that will return the class name which can be used instead of 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;
}

The final code will be something like this:

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

Here type will be the variable that distinguishes the notification.

Hope this will solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM