使用动作按钮创建GCM推送消息

7
1个回答

3

这是在设备端完成的而不是服务器端。

完成Google Cloud Messaging的完整实现后,考虑在设备上创建通知

以下是代表具有按钮的通知的代码片段,如第二个链接所指示:

Notification notification = new Notification.Builder(context)
    // Show controls on lock screen even when user hides sensitive content.
    .setVisibility(Notification.VISIBILITY_PUBLIC)
    .setSmallIcon(R.drawable.ic_stat_player)
    // Add media control buttons that invoke intents in your media service
    .addAction(R.drawable.ic_prev, "Previous", prevPendingIntent) // #0
    .addAction(R.drawable.ic_pause, "Pause", pausePendingIntent)  // #1
    .addAction(R.drawable.ic_next, "Next", nextPendingIntent)     // #2
    // Apply the media style template
    .setStyle(new Notification.MediaStyle()
    .setShowActionsInCompactView(1 /* #1: pause button */)
    .setMediaSession(mMediaSession.getSessionToken())
    .setContentTitle("Wonderful music")
    .setContentText("My Awesome Band")
    .setLargeIcon(albumArtBitmap)
    .build();

有没有办法在应用程序关闭时显示操作按钮? - Borzh
@Borzh 无论应用程序是否在前台,通知都会独立地出现在系统的通知栏上。 - madlymad
重新启动手机/模拟器,但不要启动应用程序。发送通知。您会发现什么都没有发生。这是因为一旦您启动应用程序,您的FCM服务将保持活动状态,直到下一次重启。但是当您重新启动而不打开应用程序时,该服务不处于活动状态。在这个世界上没有魔法。 - Borzh
1
@Borzh,你缺少其他代码...我建议你创建一个新问题,包括所有细节和你尝试过的内容。这是推送FCM消息的目的...从用户的角度来看,每次重新启动设备时,Facebook和其他应用程序都会向我发送通知,所以有一种方法可以做到这一点。也许你应该使用启动/重新启动广播 https://stackoverflow.com/questions/53847965/android-boot-completed-notification-not-received-after-reboot/53848560#53848560 - madlymad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接