Firebase Expandable Notification Show image when app is in background

前端 未结 12 2101
深忆病人
深忆病人 2020-12-23 14:19

I am implementing FCM notifications in Android, but how does notifications differ depending on the app status (background vs. foreground)?

12条回答
  •  余生分开走
    2020-12-23 14:48

    Update 2019 August.

    [wasted couple of days just because py doesn't support latest changes for notification]

    Just add image=url into your notification object.

    It works in Native Android. Just add image into notification object. Also please note that in Python library image field doesn't exist. [As of Aug 19] https://github.com/firebase/firebase-admin-python

    I used PHP and this library https://github.com/kreait/firebase-php/ Its super simple and more importantly, it works for big image notification when app is in background or has been killed.

    Code:

    withServiceAccount($serviceAccount)->create();
    $messaging = $firebase->getMessaging();
    
    // this works when app is closed or in bg
    $notification = Notification::fromArray([
        'title' => $title,
        'body' => $body,
        'image' => $imageUrl,
    ]);
    
    // for foreground process 
    $data = [
        'image' => $imageUrl,
        'news_id' => $news_id,
    ];
    
    $topic = 'default_topic1';
    
    $message = CloudMessage::withTarget('topic', $topic)
        ->withNotification($notification) // optional
        ->withData($data);
    
    $messaging->send($message);
    
    print_r($message);
    

提交回复
热议问题