我的UILocalNotification有问题。
我正在使用我的方法安排通知。
- (void) sendNewNoteLocalReminder:(NSDate *)date alrt:(NSString *)title
{
// some code ...
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertAction = NSLocalizedString(@"View Details", nil);
localNotif.alertBody = title;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 0;
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:stringID forKey:@"id"];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
}
工作正常,我正确收到通知。问题是我应该取消通知。我正在使用这种方法。
- (void) deleteNewNoteLocalReminder:(NSString*) reminderID noteIDe:(NSInteger)noteIDE
{
[[UIApplication sharedApplication] cancelLocalNotification:(UILocalNotification *)notification ????
}
我不知道该怎么做,但我的问题是:
我如何知道应该删除哪个UILocalNotification对象? 有没有办法列出所有通知?
我唯一拥有的是我应删除的提醒ID 我正在考虑将UILocalNotification对象保存在我的“Note”对象中并以此方式获取,当我保存到我的SQLite数据库时序列化对象等等......是否有更聪明的方法?
答案 0 :(得分:93)
我的解决方案是使用UILocalNotification
userInfo 字典。实际上,我所做的就是为每个通知生成一个唯一ID (当然这个 ID 是我以后能够检索到的),然后我想要要取消与给定 ID 相关联的通知,我只需使用该数组扫描所有可用通知:
[[UIApplication sharedApplication] scheduledLocalNotifications]
然后我尝试通过调查 ID 来匹配通知。 E.g:
NSString *myIDToCancel = @"some_id_to_cancel";
UILocalNotification *notificationToCancel=nil;
for(UILocalNotification *aNotif in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
if([[aNotif.userInfo objectForKey:@"ID"] isEqualToString:myIDToCancel]) {
notificationToCancel=aNotif;
break;
}
}
if(notificationToCancel) [[UIApplication sharedApplication] cancelLocalNotification:notificationToCancel];
我不知道这种方法在存档/取消存档方面是否更好,但它可以工作并将数据限制为只保存为ID。
编辑:缺少braket
答案 1 :(得分:57)
您可以从scheduledLocalNotifications获取所有预定通知的列表,也可以全部取消:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
答案 2 :(得分:13)
答案 3 :(得分:6)
我的解决方案是,当您创建UILocalNotification
时,您创建一个NSMutableDictionary
并将该通知存储为密钥的值作为您的ID并将此NSMutableDictionay
放入您的NSUserDefaults
1}}
因此,当您想要取消任何特定的本地通知时,您需要编写
[dictionary valueforkey @"KEY"]
作为密钥,您传递了您的ID,以便获得特定的本地通知并将其传递给
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
答案 4 :(得分:3)
在swift中,您首先通过以下方式将通知添加到通知userInfo:
let dict:NSDictionary = ["ID" : "someString"]
notification.userInfo = dict as! [String : String]
然后,您希望获得一系列现有通知(2),并遍历每个通知(3),直到找到您要查找的通知。找到后,取消它(4)。
// 1. Pass in the string you want to cancel
func cancelLocalNotification(uniqueId: String){
// 2. Create an array of notifications, ensuring to use `if let` so it fails gracefully
if let notifyArray = UIApplication.sharedApplication().scheduledLocalNotifications {
// 3. For each notification in the array ...
for notif in notifyArray as [UILocalNotification] {
// ... try to cast the notification to the dictionary object
if let info = notif.userInfo as? [String: String] {
// 4. If the dictionary object ID is equal to the string you passed in ...
if info["ID"] == uniqueId {
// ... cancel the current notification
UIApplication.sharedApplication().cancelLocalNotification(notif)
}
}
}
}
}
答案 5 :(得分:2)
Swift 版本:
package com.example.flori.mylordring;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{
private static ImageButton imgView;
private int test;
int [] images = {R.drawable.im1,R.drawable.im2,R.drawable.im3,R.drawable.im4,R.drawable.im5,R.drawable.im6,R.drawable.im7};
private ActionBarDrawerToggle actionBarDrawerToggle;
private DrawerLayout drawerLayout;
private ListView navList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
navList = (ListView)findViewById(R.id.navlist);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ArrayList<String> navArray = new ArrayList<String>();
navArray.add("Home");
navArray.add("Fragment 1");
navArray.add("Fragment 2");
navArray.add("Fragment 3");
navArray.add("Fragment 4");
navArray.add("Fragment 5");
navList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,navArray);
navList.setAdapter(adapter);
actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,R.string.opendrawer,R.string.closedrawer);
drawerLayout.setDrawerListener(actionBarDrawerToggle);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Rolololo", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
protected void onPostCreate(Bundle saveInstancesState){
super.onPostCreate(saveInstancesState);
actionBarDrawerToggle.syncState();
}
public void image(View view){
imgView=(ImageButton)findViewById(R.id.imageButton);
imgView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
test++;
test = test % images.length;
imgView.setImageResource(images[test]);
}
}
);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}else if(id == android.R.id.home){
if(drawerLayout.isDrawerOpen(navList)){
drawerLayout.closeDrawer(navList);
}else{
drawerLayout.openDrawer(navList);
}
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
}
}
}
答案 6 :(得分:1)
使用userinfo取消特定“本地通知”的Swift版本。:
func cancelLocalNotification(UNIQUE_ID: String){
var notifyCancel = UILocalNotification()
var notifyArray = UIApplication.sharedApplication().scheduledLocalNotifications
for notifyCancel in notifyArray as! [UILocalNotification]{
let info: [String: String] = notifyCancel.userInfo as! [String: String]
if info[uniqueId] == uniqueId{
UIApplication.sharedApplication().cancelLocalNotification(notifyCancel)
}else{
println("No Local Notification Found!")
}
}
}
答案 7 :(得分:1)
迅速5:
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: arrayContainingIdentifiers)
答案 8 :(得分:0)
我的解决方案是删除这些方法的所有预定通知。
-applicationDidFinishLaunchingWithOptions:
- (void)applicationDidBecomeActive:(UIApplication *)application;
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;
带
[[UIApplication sharedApplication] cancelAllLocalNotifications];
您仍然可以使用。
访问激活您的应用的LocalNotification对象 UILocalNotification *localNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsLocalNotificationKey];
然后我在
上重新安排新的通知- (void)applicationDidEnterBackground:(UIApplication *)application;
这可以避免对所有通知进行簿记。我也在userInfo字典中发送一些上下文信息。这有助于跳转到应用程序中的正确位置。
答案 9 :(得分:0)
感谢@ viggio24的好答案,这是一个快速的版本
var notifyCancel = UILocalNotification()
var notifyArray=UIApplication.sharedApplication().scheduledLocalNotifications
var i = 0
while(i<notifyArray.count){
if let notify = notifyArray[i] as? UILocalNotification{
if let info = notify.userInfo as? Dictionary<String,String> {
if let s = info["name"] {
if(name==s){//name is the the one you want cancel
notifyCancel = notify
break
}
}
}
}
i++
}