Refreshing Widget from Notification Extension

I would like to refresh Widget when application receives push notification. It sounds like a typical case that is also mentioned in many guides e.g "Keeping a Widget Up To Date". Currently, I'm trying to call
Code Block language
WidgetCenter.shared.reloadTimelines(ofKind: "MyKindOfWidget")

from Notification Extension (I receive rich push notification) but it doesn't "force" widget to reload. Calling the same code from the main app works as expected. Am I missing something?



Replies

Similar issue. Not on notification but I would need to reload the timeline (in order to change some content passed by the app itself).

reload doesn't reload with new data.

In the code, Util is to have a singleton to share data with the App.

Code Block
struct LoadStatusProvider: TimelineProvider {
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), loadEntry: 0, message: Util.shared.globalToPass)
}
func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) {
let entry = SimpleEntry(date: Date(), loadEntry: 0, message: Util.shared.globalToPass)
completion(entry)
}
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
var entries: [SimpleEntry] = []
// Generate a timeline consisting of five entries an hour apart, starting from the current date.
let currentDate = Date()
for minuteOffset in 0 ..< 2 {
let entryDate = Calendar.current.date(byAdding: .minute, value: 5*minuteOffset, to: currentDate)!
let entry = SimpleEntry(date: entryDate, loadEntry: minuteOffset, message: Util.shared.globalToPass)
print(Util.shared.globalToPass)
entries.append(entry)
}
let timeline = Timeline(entries: entries, policy: .atEnd) // .after(currentDate.addingTimeInterval(300.0))) // .atEnd)
completion(timeline)
}
}
struct SimpleEntry: TimelineEntry {
let date: Date
let loadEntry: Int
let message: String
}

I have the same problem. I have tried using a silent push notification, that way the app does get woken up in the background and I tried calling the WidgetCenter.shared.reloadTimelines method right from there. The result was the same, the widget did not get updated if the application is not in the foreground.
  • Could you share your implementation that how to use a silent push notification in widget extension?

  • Could you please share the implementation that how you receive silent push notification in widget extension and the test payload?

Add a Comment