在Android中提供来自通知的类的待处理意图

时间:2013-07-10 04:17:03

标签: 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();
    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));

如何找到错误?

修改

如果应用程序在前台接收到通知,则会收到通知,但如果活动未运行或已完成,并且我在通知时调用它,则不会发生任何事情

2 个答案:

答案 0 :(得分:1)

generateNotification中的代码只会创建通知,而不是广播。

您的接收者将永远不会收到任何东西,因为您从未广播过。要以您使用它的方式使用接收器,您需要编写与此类似的代码

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方法向用户显示错过的通知。之后,我从数据库中删除了通知。

答案 1 :(得分:1)

您写道:

  

如果应用程序在前台接收时收到通知   收到的通知很好但是如果活动没有运行或   它已经完成,我在通知时调用它点击没有发生的事情

如果您通过致电:

在活动中注册接收器
registerReceiver(mHandleMessageReceiver, new IntentFilter(CommonUtilities.DISPLAY_ACTION));

然后您使用活动的上下文注册了接收器。这意味着当活动结束时,已注册的接收器将被删除并销毁(以防止内存泄漏)。

如果您希望即使您的应用未运行也能运行接收器,那么您需要在清单中注册接收器,方法是在<receiver>定义中添加适当的意图过滤器:

        <intent-filter>
            <!-- use the correct name string for CommonUtilities.DISPLAY_ACTION) -->
            <action android:name="blah.blah.blah.DISPLAY_ACTION"/>
        </intent-filter>