android - 在 BroadcastReceiver 的 onReceive() 中创建 PendingIntent

标签 android push-notification google-cloud-messaging

有一个BroadcastReceiver,我想在onReceive 方法中创建一个PendingIntent

public class MyPushNotificationReceiver extends BroadcastReceiver {
..
    public void onReceive(Context context, Intent intent) {

        // How to start a PendingIntent here?

    }

Google 的大部分文档都不是从 onReceive 方法开始的,所以我可以使用任何示例代码吗?

谢谢。

最佳答案

在广播接收器中使用挂起 Intent 的示例代码请检查

    public class MyScheduleReceiver extends BroadcastReceiver {

// Restart service every 30 minute
private static final long REPEAT_TIME = 1000 * 30 ;

@Override
public void onReceive(Context context, Intent intent) {
    AlarmManager service = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, MyStartServiceReceiver.class);
    PendingIntent pending = PendingIntent.getBroadcast(context, 0, i,
            PendingIntent.FLAG_CANCEL_CURRENT);
    Calendar cal = Calendar.getInstance();
    // Start 30 seconds after boot completed
    cal.add(Calendar.SECOND, 30);
    //
    // Fetch every 30 seconds
    // InexactRepeating allows Android to optimize the energy consumption
    service.setInexactRepeating(AlarmManager.RTC_WAKEUP,
            cal.getTimeInMillis(), REPEAT_TIME, pending);

    // service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
    // REPEAT_TIME, pending);

}
     }

此代码来自 vogella ...

关于android - 在 BroadcastReceiver 的 onReceive() 中创建 PendingIntent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12070704/

相关文章:

android - 将 Parse-1.8.0 添加到 Android Studio 1.0.1(或任何 .zip 文件)

android - 如何解决flutter_local_notifications PlatformException错误?

android - 当我的应用程序启动时,发生强制关闭错误

ios - 在不运行应用程序或发送推送通知的情况下标记 iOS 应用程序?

push-notification - Webrtc 通过 FCM 发送通话报价

java - 观察数据库中的@Relation 变化

android - 通过我的网站向特定的安卓设备发送通知

android - 一旦将预期的消息传递到 android 应用程序,我的服务器能否收到来自 GCM 的确认?

c# - Azure 通知中心错误 401

android - 如何手动生成 google-services.json