android: context has some problem when displaying push notification using C2DM

帅比萌擦擦* 提交于 2019-12-11 11:59:19

问题


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(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.

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

AlertDialog

//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);

Starting another activity

//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); 

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


回答1:


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.

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.

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...



来源:https://stackoverflow.com/questions/6921464/android-context-has-some-problem-when-displaying-push-notification-using-c2dm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!