- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个推送通知应用程序。当推送通知到来时,BroadcastReceiver 调用 GCMIntentService 设置通知
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, HomeActivity.class);
Intent intent = new Intent(this, AddToCalendarIntentService.class);
intent.putExtra(JSON_KEY_CLASS_ID, classid);
intent.putExtra(JSON_KEY_CLASS_NAME, classname);
intent.putExtra(Config.JSON_KEY_DATE, tgl);
intent.putExtra(Config.JSON_KEY_TIME_START, timestart);
intent.putExtra(Config.JSON_KEY_TIME_END, timeend);
intent.putExtra(Config.JSON_KEY_VENUE, venue);
Log.d(LOG_TAG, classid + ", " + classname + ", " + venue);
PendingIntent contentIntent = PendingIntent.getService(this, 0,
intent, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notif)
.setContentTitle("Re: " + classname)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(pesan))
.setContentText(pesan)
.setDefaults(NotificationCompat.DEFAULT_VIBRATE)
.setDefaults(NotificationCompat.DEFAULT_LIGHTS)
.setDefaults(NotificationCompat.DEFAULT_SOUND)
.addAction(R.drawable.ic_action_add_person, "Add to Calendar", contentIntent)
.setAutoCancel(true)
.setTicker("Reschedule Class");
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
当用户单击“添加到日历”时,它会调用 PendingIntent 以使用所有参数启动 AddToCalendarIntentService。
public class AddToCalendarIntentService extends IntentService {
public AddToCalendarIntentService() {
super("AddToCalendarIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
final String JSON_KEY_CLASS_NAME = "classname";
final String JSON_KEY_CLASS_ID = "classid";
Bundle extra = intent.getExtras();
String title = extra.getString(JSON_KEY_CLASS_NAME);
String location = extra.getString(Config.JSON_KEY_VENUE);
String tgl = extra.getString(Config.JSON_KEY_DATE);
String[] tglSplit = tgl.split("-");
int year = Integer.parseInt(tglSplit[0]);
int month = Integer.parseInt(tglSplit[1]);
int date = Integer.parseInt(tglSplit[2]);
String timestart = extra.getString(Config.JSON_KEY_TIME_START);
String timeend = extra.getString(Config.JSON_KEY_TIME_END);
String[] start = timestart.split(":");
String[] end = timeend.split(":");
int hourStart = Integer.parseInt(start[0]);
int minStart = Integer.parseInt(start[1]);
int hourEnd = Integer.parseInt(end[0]);
int minEnd = Integer.parseInt(end[1]);
Log.d("INTENT SERVICE", location);
TaskHelper.addToCalendar(this, "Reschedule: " + title, location, year, month-1, date, hourStart, minStart, hourEnd, minEnd);
NotificationManager mNotification = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotification.cancel(1);
}
}
代码有效,但如果我再次收到另一个通知,并将其添加到日历中,AddToCalendarIntentService 中的参数将保留旧参数,忽略新参数。例如,第一个位置是印度尼西亚,GCMIntentService 和AddToCalendarIntentService 日志都显示印度尼西亚。
在第二个通知中,位置是新加坡,它在GCMIntentService中正确显示,但是在登录AddToCalendarIntentService时,它显示印度尼西亚(应该是新加坡)
请帮忙。
谢谢。
最佳答案
这是因为系统认为它是同一个 PendingIntent,如果你想有几个 pending Intent,你需要区分它们。
最简单的方法是提供不同的请求代码(PendingIntent.getService 的第二个参数)。
您可以在这里阅读更多相关信息:
http://developer.android.com/reference/android/app/PendingIntent.html
关于Android:从 NotificationManager 操作 PendingIntent 调用 IntentService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26688937/
我想了解 PendingIntent.FLAG_NO_CREATE 的行为。我想知道它什么时候返回 null 什么时候不返回。这是事件发生的顺序: alarmIntent = new Intent(c
我正在尝试请求网络,因此我构建了 NetworkRequest 并构建了 PendingIntent, ConnectivityManager conn_manager = (Connectivit
我一直在使用警报管理器。一旦我设置了一个 PendingIntent 来触发警报,我想取消它。 How do I achieve this? 帮助将不胜感激。 最佳答案 下面这几行代码肯定可以帮助您删
我正在尝试在 Android 中创建一个 PendingIntent。这是代码 mNotificationIntent = new Intent(getApplicationContent(), My
我有三个 Activity (Home、Search、Destination),我可以用它们来描述我的用户体验流程。 Home Activity 是我的启动器 Activity ,然后是搜索 Acti
我试图从 API 中了解 PendingIntent 的用途。 有时我使用的一些方法需要这样做,但我仍然没有得到正确的想法。 谁能解释一下吗?为什么我不能只使用上下文? 谢谢 莫希克 最佳答案 Pen
我在 Android 中开发闹钟时遇到问题。当我触发包含必须在计划时间启动的 Activity 的 PendingIntent 时,它运行良好。但是,当闹钟响起并且我按贪睡 5 分钟并使用不同的请求代
我得到了一个 IntentService,它在 onHandleIntent(Intent) 中同步执行一些长时间运行的工作。因此,只要服务运行,我就会显示一个通知。现在我想在用户点击通知后停止服务。
我有这个... button = (Button) findViewById(R.id.start_repeating); button.setOnClickListener(new OnClickL
在我的应用程序中,我有一个显示 2 个选项的对话框:Copy 和 Dismiss 当用户单击 Copy 时,我希望在状态栏中收到通知。 public void showDialog(final Str
我有一个会显示通知的服务。通知显示正确,但当我点击通知时没有任何反应。它应该在我的应用程序中打开一个 Activity 。这是我的代码: public class ServiceCheckExpens
我正在我的游戏中创建一个通知。如果玩家有一段时间没有玩游戏,我想显示通知。我正在使用以下带有警报设置的 PendingIntent 来创建通知。有了这个,我就可以创建通知。 问题:如果玩家玩游戏,我需
所以这段代码是在我的服务开始在前台启动后从内部运行的。这也会创建将留在那里的通知。我有很多问题/疑问: 1) 点击通知只会打开应用信息,不会打开我的 PendingIntent。 2) 未设置我的内容
如何创建多个 PendingIntent?我有几个通知,当用户按下最后一个通知时,一切正常,但点击通知没有响应。我的代码是: Intent notificationIntent = new Inten
我正在运行一项服务,该服务会在特定事件的通知栏上发出通知。在该通知中,我设置了一个 PendingIntent 以启动另一个应用程序。出于某种原因,当我根据通知采取行动时,它不会启动其他应用程序。这是
我有一个名为 sendNotification 的方法,可以多次调用。 private PendingIntent createNotificationIntent(Context context,
我有一个导航应用程序。我想获得可能暂时不会到达的位置信息,具体取决于 GPS 锁定所需的时间——或者接收信号不好时的时间。 我计划使用 LocationManager.requestLocationU
我有一个 Android 库,它有一个创建 Notification 的 Service。据我了解,Notification 必须有一个 contentIntent (PendingIntent) 集
当我们将 0 作为标志传递给 PendingIntent 时,如下所示: PendingIntent pi=PendingIntent.getActivity(this, 1, i, 0); does
我在尝试通过 Intent 和未决 Intent 将数据传递给 BroadcastReceiver 时遇到了一些问题,涉及接近警报。更具体地说,我试图传递一个对象,其中包括用户不断变化的位置。我已经尝
我是一名优秀的程序员,十分优秀!