[英]Supply pending intent to class from notification in Android
我正在使用此代碼向我的應用程序發送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));
我怎樣才能找到錯誤?
如果應用程序在前台接收到通知,則會收到通知,但如果活動未運行或已完成,並且我在通知時調用它,則不會發生任何事情
您在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
方法向用戶顯示錯過的通知。 之后,我從數據庫中刪除了通知。
你寫了:
如果應用程序在前台接收到通知,則會收到通知,但如果活動未運行或已完成,並且我在通知時調用它,則不會發生任何事情
如果您通過以下方式在活動中注冊接收器:
registerReceiver(mHandleMessageReceiver, new IntentFilter(CommonUtilities.DISPLAY_ACTION));
然后你使用活動的上下文注冊了接收者。 這意味着當活動結束時,已注冊的接收器將被刪除並銷毀(以防止內存泄漏)。
如果您希望即使應用程序未運行也能運行接收器,則需要在清單中注冊接收器,方法是在<receiver>
定義中添加適當的intent過濾<receiver>
:
<intent-filter>
<!-- use the correct name string for CommonUtilities.DISPLAY_ACTION) -->
<action android:name="blah.blah.blah.DISPLAY_ACTION"/>
</intent-filter>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.