机器人:上下文都有显示使用C2DM推送通知时,一些问题 [英] android: context has some problem when displaying push notification using C2DM

查看:117
本文介绍了机器人:上下文都有显示使用C2DM推送通知时,一些问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用显示C2DM推送通知信息。如果这是一个烤面包的消息,那么它显示的罚款。但是,当我尝试了状态栏通知,或者一些AlertDialog它不工作。同时,我想开始消息的接收另一个活动,那么它也不能正常工作。我试着用2种情况下的:

I am trying to display push notification messages using C2DM. If it is a Toast message, then it is showing fine. But when I try for Status Bar Notification, or some AlertDialog it is not working. And also I tried to start another activity on the reception of message, then also it was not working. I tried with the 2 types of context :


  1. 的onReceive(上下文的背景,意图意图)

  2. 我创建了第一个活动一个静态变量,并尝试使用该变量来获取应用程序contaxt。

  1. onReceive(Context context, Intent intent)
  2. I created a static variable on the first activity, and tried to get the application contaxt using that variable.

但是,没有什么是给了预期的效果。

But nothing is giving the desired results.

以下是code的状态栏通知,AlertDialog,并开始其他活动。

Following are the code for StatusBar Notification, AlertDialog, and to start another activity.

//testing for dialog
AlertDialog.Builder dialog = new AlertDialog.Builder( C2dmRegistration.obj.getBaseContext());
dialog.setMessage("New message : "+message);
dialog.show();
dialog.setNeutralButton("View", new OnClickListener()       
{

    @Override
    public void onClick(DialogInterface dialog, int which)
     {

    }
});

StatusBarNotification

//testing for ststus bar notifications
NotificationManager objNotfManager=(NotificationManager)  C2dmRegistration.obj.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.logo;
CharSequence tickerMessage = "Hello";
long when= System.currentTimeMillis();
Notification objNotf = new Notification(icon,tickerMessage,when);
//Context context = getApplicationContext();
CharSequence title = "Welcome";
CharSequence mesage = "have a nice day";
Intent NotifIntent  = new Intent(context,NotificationShow.class);
PendingIntent contentIntent  = PendingIntent.getActivity( C2dmRegistration.obj.getApplicationContext(), 0, NotifIntent, 0);            
objNotf.setLatestEventInfo( C2dmRegistration.obj.getApplicationContext(), title, mesage, contentIntent);
objNotfManager.notify(1,objNotf);

启动另一个活动

//testing for new activity
Intent startActivity = new Intent();     
startActivity.setClass( C2dmRegistration.obj.getApplicationContext(), NotificationShow.class);       
startActivity.setAction(NotificationShow.class.getName());       
startActivity.setFlags(  Intent.FLAG_ACTIVITY_NEW_TASK  | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);  
startActivity.putExtra("Title", "Hello");       
startActivity.putExtra("Message", message);          
context.startActivity(startActivity); 

我想AP preciate任何帮助...
谢谢...

I would appreciate any help... Thank you...

推荐答案

首先,与上下文静态变量是不是要走的路。 C2DM子系统能够开始你的应用程序时,它没有启动的主要活动运行。因此,静止无功不会被初始化。

For one thing, static variable with the context is NOT the way to go. C2DM subsystem is capable of starting your app process when it's not running without starting the main activity. So the static var won't be initialized.

在我的经验,你通过的onReceive获取上下文()是完全没有问题。状态栏通知,但是,是出了名的挑剔和细微错误的情况下不抛出异常。我建议你​​调试那些活动(只是弹出一起来的的onCreate,例如),然后将code到C2DM接收器。

In my experience, the context that you get via onReceive() is perfectly fine. Status bar notifications, however, are notoriously picky and don't throw exceptions in case of subtle errors. I suggest you debug those from the activity (just pop one up in onCreate, for example), then move the code into the C2DM receiver.

时的图像 R.drawable.logo 实际上present项目?我有一个令人沮丧的调试会话一次,这是有关缺失通知图标...

Is the image R.drawable.logo actually present in the project? I had a frustrating debug session once that was about a missing notification icon...

这篇关于机器人:上下文都有显示使用C2DM推送通知时,一些问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆