我想在点击收到的通知时打开我的mainActivity

时间:2013-10-20 21:17:03

标签: android android-activity push-notification

我正在使用Android应用程序,我已经在PushBots通知服务中注册,并且我已成功接收到手机上的通知。

但是当我点击通知时没有任何事情发生..

我想要的是,当用户点击通知时,我想打开我的MainActivity。

我创建了3个java文件:

1)MainActivity.java

2)MyApplication.java

3)customPushReceiver.java

我的问题是我应该在哪里编写MainActivity点击通知时开始的代码?我应该写什么代码??

这是我所有的三个类代码。

1)MainActivity.java代码 -

 package com.example.ptest2;

import com.pushbots.push.Pushbots;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

private static final Context Context = null;

private String SENDER_ID="My_SENDER_ID";
private String PUSHBOTS_APPLICATION_ID="MY_Application_ID";


@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


}



}

2)MyApplication.java代码 -

package com.example.ptest2;

import android.widget.Toast;

import com.pushbots.push.Pushbots;



public class MyApplication extends android.app.Application 
{
 private String SENDER_ID="MY_SENDER_ID";
private String PUSHBOTS_APPLICATION_ID="MY_Application_ID";

@Override
    public void onCreate() 
{
        super.onCreate();


        Pushbots.init(this, SENDER_ID,PUSHBOTS_APPLICATION_ID);

     }

}

3)customPushReceiver.java代码 -

package com.example.ptest2;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.BroadcastReceiver;

import java.awt.font.TextAttribute;
import java.util.HashMap;
import com.pushbots.push.Pushbots;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Context;
import android.content.Intent;

public class customPushReceiver extends BroadcastReceiver 
{
 private static final String TAG = "customPushReceiver";


@Override
public void onReceive(Context context, Intent intent) 
{

    Toast.makeText(context, "Your time is up", Toast.LENGTH_LONG).show();




     Vibrator v;
     v=(Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
     v.vibrate(3000);
     }
 }

onReceive方法正在成功运行..

我的问题是我应该在哪里编写MainActivity点击通知时开始的代码?我应该写什么代码??

2 个答案:

答案 0 :(得分:3)

这是一个完整的视频教程。 http://www.youtube.com/watch?v=fK46FJahvs0

答案 1 :(得分:1)

您可以在customPushReceiver.java类中创建generateNotification()方法。 在onReceive()中调用generateNotification():

    private void generateNotification(Context context, String message,
        long when, String query) {

    int icon = R.drawable.icon;
         long when = System.currentTimeMillis();
    String appname = context.getResources().getString(R.string.app_name);
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    Notification notification;

    Intent intent = new Intent(context, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            intent, 0);

        if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
            notification = new Notification(icon, message, when);
            notification.setLatestEventInfo(context, appname, message,
                    contentIntent);
            notification.flags = Notification.FLAG_AUTO_CANCEL;
            notificationManager.notify((int) when, notification);

        } else {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(
                    context);
            notification = builder.setContentIntent(contentIntent)
                    .setSmallIcon(icon).setTicker(appname).setWhen(when)
                    .setAutoCancel(true).setContentTitle(appname)
                    .setContentText(message).build();

            notificationManager.notify((int) when, notification);

        }

}

希望这有帮助。