Android - use external profile image in notification bar like Facebook

后端 未结 5 1486
眼角桃花
眼角桃花 2020-11-30 06:37

I know you can send info in the push notification parameters like message, title, image URL, etc. How does Facebook show your profile pic with your message in the notificati

5条回答
  •  温柔的废话
    2020-11-30 07:15

    I used Universal Image Loader to solve this. Take a look at the wiki on how to set it up. After you instantiate it once, this is the code I use in my GCM listener to download and display the image. I download the bitmap and then set it in the notification:

    // Download profile picture of the user with Universal Image Loader
    Bitmap bitmap =  ImageLoader.getInstance().loadImageSync(profilePhotoUrl);
    
    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_my_app)
            .setLargeIcon(bitmap) // This is the image displayed on the lock screen
            .setContentTitle("My App")
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
    

提交回复
热议问题