由GCM推送Android应用程序的通知(Push notification for android app by GCM)

我正在研究Android应用程序的事件提醒,这个应用程序只显示事件列表和有关它的详细信息..某些事件的日期是更改。

我需要推送通知以提醒用户有关活动日期的信息

阅读Google云端消息传递(GCM)后,我感到很困惑

  • 是否有必要要求用户通过应用程序输入姓名或电子邮件? 我不需要那个!

  • 我在哪里可以写一个新的通知“消息”来发送它?

谢谢 ,


I am working on android app for events reminder , this app only display event as list and details about it .. some event's date is change .

I need to push notification to remind a user about event date

I'am Confused when read about Google Cloud Messaging ( GCM )

  • Is it necessary to request from the user to enter name or email through the application ? I don't need that !

  • where can I write a new notification "message" to send it ?

Thank you ,


原文:https://stackoverflow.com/questions/35894906
2023-04-28 18:04

满意答案

你正在用另一个影子你的for-loop i

for i in range(1,3):
    # type(i) is int
    …
    for i in scan1:
        …
    # type(i) is str
    …
    diskchange[i] = disk # i is str() here, so it yells at you!

将你的内循环索引从i更改为j ,你应该没问题。

在python中,变量的可见性超过了一个完整的函数,而不是一个块。 因此,当您在块中定义变量时,它在块之后仍然可见:

 i = 'foobar'
 print(i, type(i))
 for i in range(100):
     pass
 print(i, type(i))

会给:

foobar str
100 int

因此在重用变量名时要小心。

要解决您的问题,只需将索引名称更改为j

for j in scan1:
    if j not in scan2:
            result = result + j

奖金:

在下面的代码片段中,您将通过将整个内容发送到shell来读取脚本的完整内容以执行它:

with open('removeslot%d.sh' %i, 'rb') as f:
    script = f.read()
subprocess.call(script, shell=True)

这实际上是过度设计,你可以简单地做:

subprocess.call('removeslot%d.sh' % i, shell=True)

执行脚本(或'bash removeslot%d.sh' % i ,以防您的.sh不可执行)。 还要注意你再次遮蔽一个符号: file是用于处理全局可用文件的类。 改用f

HTH


You're shadowing your for-loop i with another i:

for i in range(1,3):
    # type(i) is int
    …
    for i in scan1:
        …
    # type(i) is str
    …
    diskchange[i] = disk # i is str() here, so it yells at you!

Change your inner loop's index from i to j and you should be fine.

In python, the variable's visibility is over a full function, not a block. So when you're defining a variable within a block, it's still visible after the block:

 i = 'foobar'
 print(i, type(i))
 for i in range(100):
     pass
 print(i, type(i))

will give:

foobar str
100 int

so be careful when reusing variable names.

To fix your issue, just change the index name to j:

for j in scan1:
    if j not in scan2:
            result = result + j

bonus:

In the following snippet, you're reading the full content of a script to execute it by sending its whole content to a shell:

with open('removeslot%d.sh' %i, 'rb') as f:
    script = f.read()
subprocess.call(script, shell=True)

this is actually over engineered, you can simply do:

subprocess.call('removeslot%d.sh' % i, shell=True)

to execute the script (or 'bash removeslot%d.sh' % i in case your .sh is not executable). Also beware that you're again shadowing a symbol: file is the class for handling files that is globally available. Use f instead!

HTH

相关问答

更多

Python - > TypeError:list indices必须是整数,而不是str(Python -> TypeError: list indices must be integers, not str)

解释者永远不会错...... 更严重的是,您将num作为子字符串,因此它是一个字符串。 如果要将其用作字符串索引,则必须将其转换为int: num = int(posArray) # ok num is now an int print temp+"("+str(num)+")" # must use str to concat it with strings print num, type(num), posArray, type(posArray) # num ...

Python:列表索引必须是整数,而不是str?(Python: list indices must be integers, not str?)

prof是proflist的元素,而不是索引。 替代 if line[1].lower()==proflist[prof]: 同 if line[1].lower() == prof: prof is an element of proflist, not an index. Substitute if line[1].lower()==proflist[prof]: with if line[1].lower() == prof:

Python chatbot - TypeError:list indices必须是整数,而不是str(Python chatbot - TypeError: list indices must be integers, not str)

self.database是一个列表。 通过指定列表中的位置来访问列表项,例如第一个项的self.database[0]和第五个项目的self.database[4] 。 您试图将text用作列表位置,这没有任何意义。 如果要基于文本键而不是整数位置存储项目,请使用字典而不是列表。 self.database is a list. List items are accessed by specifying their position within the list, for example se...

直方图:“TypeError,列表索引必须是整数,而不是str”(Histograms: “TypeError, list indices must be integers, not str”)

该错误是因为您正在尝试将键分配给list ,而list只能由整数list[0] , list[1]索引,依此类推。 因此, hinstogram必须是dict而不是list 确保在调用add_to_hist方法时,传递一个字典。 你可以用这种方式初始化一个字典: histogram = {} 更新 根据你的评论,你不能将[['B',1],['a',3],['n',2],['!',1]]作为参数传递给add_to_his ,因为不是一个字典。 它应该是{'B':1,'a':3,'n':2,'!':...

带有Dataframe的TypeError:列表索引必须是整数或切片,而不是str(TypeError with Dataframe: list indices must be integers or slices, not str)

你用一个香草Python列表覆盖了你的DF: df = [['Adj. Open','Adj. High','Adj. Low','Adj. Close','Adj. Volume']] 试试这个: In [102]: df = df.filter(regex='^Adj\.') In [103]: df Out[103]: Adj. Open Adj. High Adj. Low Adj. Close Adj. Volume Date 2004-08-...

TypeError:列表索引必须是整数,而不是str(实际上是boolean convertion)(TypeError: list indices must be integers, not str (boolean convertion actually))

只需改变需要做的事情就是必须将features初始化为dict ( {} )而不是list ( [] ),然后才能填充其内容。 TypeError是因为word_features是您尝试使用列表进行索引的字符串列表,列表不能包含字符串索引。 features={} for w in word_features: features[w] = (w in words) 这里, word_features存在的元素构成字典的keys ,保存布尔值的特征,基于相同元素是否出现在words (其中...

Python3 TypeError:列表索引必须是整数或切片,而不是str(Python3 TypeError: list indices must be integers or slices, not str)

首先,在内置python语句或数据结构(如list , tuple或引用的模块的名称)之后,不要将变量命名,这也适用于文件。 例如命名你的文件socket.py并导入socket模块肯定会导致错误(我会让你自己尝试) 在你的代码element是一个字符串,迭代的索引必须是数字而不是字符串,所以你可以告诉python 给我2号位的物品。 但是现在你试着说给我A位置的项目,这在英语中是无效的,没有谈论编程语言。 你应该使用enumerate函数,如果你想循环迭代的时候获得迭代的索引,或者你可以这样做 ...

在列表上混淆:TypeError:list indices必须是整数,而不是str(Confused on lists: TypeError: list indices must be integers, not str)

你正在用另一个影子你的for-loop i : for i in range(1,3): # type(i) is int … for i in scan1: … # type(i) is str … diskchange[i] = disk # i is str() here, so it yells at you! 将你的内循环索引从i更改为j ,你应该没问题。 在python中,变量的可见性超过了一个完整的函数,而不是一个块。...

TypeError:list indices必须是整数或切片,而不是str dictionary python(TypeError: list indices must be integers or slices, not str dictionary python)

这条线: res = sorted(res) 并没有回归你的想法。 在字典上使用sort将对其键进行排序并将其作为列表返回。 当您在上下文管理器中执行res[key] ,您将使用字符串为列表编制索引,从而导致错误。 如果您想在字典中订购,可以通过以下两种方式之一进行订购: 重命名您创建的列表: sorted_keys = sorted(res) 然后在索引静止引用dict名称res同时迭代这些。 或者,使用OrderedDict ,然后像使用普通字典一样遍历其成员: from collecti...

TypeError:列表索引必须是整数,而不是Flask App中的str(TypeError: list indices must be integers, not str in Flask App)

您有一个包含字典的元组列表。 无法使用字符串访问,您需要使用整数。 最初的Django应用程序使用了一种名为SortedDict的特殊类型的Django字典,幸好Python自2.7以来就有一个名为OrderedDict的等价字典。 你只需要像这样导入它 from collections import OrderedDict 然后调整语法以使用OrderedDict并删除值周围的_() : meters_info = OrderedDict([ ("instance", ...

相关文章

更多

nslocal notification

notification.alertBody = [NSString stringWithFormat ...

开发android App干坏事(一)

最近都是在搞java,android的知识,前两天生日朋友和我聊到,有一个认识的人通过反编译andro ...

android APP 中微信分享功能实现 的总结

//花了很长时间最终完成了微信分享功能,中间走了很多弯路,在此做一下小结,希望对在应用中使用到微信分享 ...

android APP 中微信分享功能实现 的总结

//花了很长时间最终完成了微信分享功能,中间走了很多弯路,在此做一下小结,希望对在应用中使用到微信分享 ...

HTML5 Notification 桌面提醒功能 API

桌面通知功能能够让浏览器即使是最小化状态也能将消息通知给用户。这和WebIM是最为天然的结合。不过,目 ...

Android 社交类APP 豆瓣同城Lite(安全,无广告)

随着科技的发展,人们的生活越来越变的单调,有时间也不知道如何打发。使用豆瓣同城手机客户端能帮助你发现身 ...

Android app开发实战第二季【公开课视频下载】

Android入门公开课到此要给大家画上一个圆满的句号啦!通过前面10次的android公开课,相信大 ...

Android app开发实战第一季【公开课视频下载】

经过前面9期的android基础入门公开课,相信大家对于android课程体系已经有一个比较全面的认识 ...

微信公众平台如何与Web App结合?

Web App简而言之就是为移动平台而优化的网页,它可以表现得和原生应用一样,并且克服了原生应用一些固 ...

微信公众平台如何与Web App结合

Web App简而言之就是为移动平台而优化的网页,它可以表现得和原生应用一样,并且克服了原生应用一些固 ...

最新问答

更多

您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)

将diff文件复制到存储库的根目录,然后执行以下操作: git apply yourcoworkers.diff 有关apply命令的更多信息, apply 见其手册页 。 顺便说一下:一个更好的方法是通过文件交换整个提交文件是发送者上的命令git format-patch ,然后在接收器上加上git am ,因为它也传送作者信息和提交信息。 如果修补程序应用程序失败,并且生成diff的提交实际上在您的备份中,则可以使用尝试在更改中合并的apply程序的-3选项。 它还适用于Unix管道,如下

将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)

尝试将第二行更改为snprintf(buf1, sizeof buf1, "%.2f", balance1); 。 另外,为什么要声明用该特定表达式分配缓冲区的存储量? EDIT @LưuVĩnhPhúc在下面的评论中提到我的原始答案中的格式说明符将舍入而不是截断,因此根据如何在不使用C舍入的情况下截断小数,您可以执行以下操作: float balance = 200.56866; int tmp = balance1 * 100; float balance1 = tmp / 100.0; c

OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)

这是简单的解决方案 在你需要写的控制器中 BackendMenu::setContext('Archetypics.Team', 'website', 'team'); 请参阅https://octobercms.com/docs/backend/controllers-views-ajax#navigation-context BackendMenu::setContext('Author.Plugin name', 'Menu code', 'Sub menu code'); 你需要在r

页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)

每当发出请求时ASP都会创建一个新的Page对象,并且一旦它将响应发送回用户就不会保留对该Page对象的引用,因此只要你找不到某种方法来保持生命自己引用该Page对象后,一旦发送响应, Page和只能通过该页面访问的所有对象才有资格进行垃圾回收。 ASP creates a new Page object whenever a request is made, and it does not hold onto the reference to that Page object once it

codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)

要在生产服务器中调试这个,你可以临时放 error_reporting(E_ALL); 并查看有哪些其他错误阻止正确的重定向。 您还应该检查生产服务器发送的响应标头。 它是否具有“缓存”,是否需要重新验证标头等 to debug this in production server, you can temporary put error_reporting(E_ALL); and see what other errors are there that prevents the proper

在计算机拍照在哪里进入

打开娥的电脑.在下面找到视频设备点击进去就可以了...

使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)

你是对的。 第一次输入后,换行符将保留在输入缓冲区中。 第一次读取后尝试插入: cin.ignore(); // to ignore the newline character 或者更好的是: //discards all input in the standard input stream up to and including the first newline. cin.ignore(numeric_limits::max(), '\n'); 您必须为#inc

No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)

for (int k = 0; k > 10; k++) { System.out.println(k); } k不大于10,所以循环将永远不会执行。 我想要什么是k<10 ,不是吗? for (int k = 0; k < 10; k++) { System.out.println(k); } for (int k = 0; k > 10; k++) { System.out.println(k); } k is not greater than 10, so loop

单页应用程序:页面重新加载(Single Page Application: page reload)

优点是不注销会避免惹恼用户,以至于他们会想要杀死你:-)。 说真的,如果每次刷新页面时应用程序都会将我注销(或者在新选项卡中打开一个链接),我再也不会使用该应用程序了。 好吧,不要这样做。 确保身份验证令牌存储在刷新后的某个位置,即不在某些JS变量中,而是存储在cookie或本地存储中。 The advantage is that not logging off will avoid pissing off your users so much that they'll want to kill

在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)

EXECUTE IMMEDIATE 'SELECT '||field_val_temp ||' FROM tableb WHERE function_id = :func_val AND rec_key = :rec_key' INTO field_val USING 'STDCUSAC' , yu.rec_key; 和, EXECUTE IMMEDIATE 'UPDATE tablec SET field_val_'||i||' = :field_val' USI