从Android中的通知向类提供挂起的意图
我正在通过使用此代码向我的应用程序发送GCM消息来创建通知从Android中的通知向类提供挂起的意图,android,android-intent,notifications,google-cloud-messaging,android-pendingintent,Android,Android Intent,Notifications,Google Cloud Messaging,Android Pendingintent,我正在通过使用此代码向我的应用程序发送GCM消息来创建通知 private static void generateNotification(Context context, int type, String title, String message) { Intent notificationIntent; int icon = R.drawable.ic_launcher; java.util.Random v = new java.util.Random();
private static void generateNotification(Context context, int type, String title, String message) {
Intent notificationIntent;
int icon = R.drawable.ic_launcher;
java.util.Random v = new java.util.Random();
int id = v.nextInt(1000);
long when = System.currentTimeMillis();
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notification = new NotificationCompat.Builder(context);
notificationIntent = new Intent(context, Home.class);
notificationIntent.putExtra(CommonUtilities.TITLE_ALERT, title);
notificationIntent.putExtra(CommonUtilities.EXTRA_MESSAGE, message);
notificationIntent.putExtra(CommonUtilities.TYPE, type);
notificationIntent.putExtra(CommonUtilities.ID, id);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, type, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
Notification notification_view = notification.setContentTitle(title)
.setContentText(message).setContentIntent(intent)
.setSmallIcon(icon).setWhen(when)
.setVibrate(new long[] { 1000 }).build();
notification_view.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification_view.defaults |= Notification.DEFAULT_SOUND;
// notification_view.sound = Uri.parse("android.resource://" +
// context.getPackageName() + "your_sound_file_name.mp3");
// Vibrate if vibrate is enabled
notification_view.defaults |= Notification.DEFAULT_VIBRATE;
manager.notify(id, notification_view);
}
并使用接收方接收该未决意图
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(!intent.hasExtra(CommonUtilities.TYPE)){
Log.v("msg", "intent vars not received");
return;
}
int type = intent.getExtras().getInt(CommonUtilities.TYPE);
String title = intent.getExtras().getString(CommonUtilities.TITLE_ALERT);
String newMessage = intent.getExtras().getString(CommonUtilities.EXTRA_MESSAGE);
String[] msgArr = newMessage.split(",");
Log.v("message", newMessage);
}
};
但是我的活动没有执行操作并向我显示日志。我已使用自定义意图注册了我的接收者
registerReceiver(mHandleMessageReceiver, new IntentFilter(CommonUtilities.DISPLAY_ACTION));
我怎样才能找到错误
编辑
如果应用程序在前台收到通知,则通知接收良好,但如果活动未运行或已完成且我在通知时调用它,请单击“未发生任何事情”生成通知中的代码将只创建通知,而不是广播 你的接收器永远不会收到任何东西,因为你从未广播过。要以使用接收器的方式使用它,您需要编写与此类似的代码
public static final String DISPLAY_ACTION = "package.name.DISPLAY_MESSAGE";
public static final String EXTRA_MESSAGE = "message";
public static void displayMessage(Context context, String message) {
Intent intent = new Intent(DISPLAY_ACTION);
intent.putExtra(EXTRA_MESSAGE, message);
context.sendBroadcast(intent);
}
编辑
我会将上面的代码添加到您的CommonUtilities
类中,您还需要将这一行添加到generateNotification
方法中
CommonUtilities.displayMessage(context, message);//this will then send your broadcast to the receiver.
编辑-打开应用程序时显示通知消息
我在我的应用程序中使用了类似的功能。当GCM
收到通知时,我将其保存在数据库中,作为未读通知,然后提醒用户,一旦打开应用程序,我会检查未读通知,如果发现,则调用displayMessage
方法向用户显示错过的通知。之后,我从数据库中删除通知。您写道:
若应用程序在前台收到通知,则
通知接收良好,但如果活动未运行或
它完成了,我在通知时调用它。单击“未发生任何事情”
如果您通过拨打以下电话从活动中注册收件人:
registerReceiver(mHandleMessageReceiver, new IntentFilter(CommonUtilities.DISPLAY_ACTION));
然后,您已经使用活动的上下文注册了接收者。这意味着当活动完成时,注册的接收器将被移除并销毁(以防止内存泄漏)
如果您希望在应用程序未运行的情况下运行接收器,则需要在清单中注册接收器,方法是在
定义中添加适当的意图过滤器:
<intent-filter>
<!-- use the correct name string for CommonUtilities.DISPLAY_ACTION) -->
<action android:name="blah.blah.blah.DISPLAY_ACTION"/>
</intent-filter>
是的,我已经在这样做了,我正在发送一个广播意图,但什么都没有发生……你把sendBroadcast称为哪里,因为我看不到它?查看我的编辑您从哪里调用registerReceiver()
?什么成分?