0

我正在开发一个 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 将在单击通知时开始的代码?我应该写什么代码?

4

2 回答 2

3

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

于 2013-10-29T09:10:01.183 回答
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);

        }

}

希望这可以帮助。

于 2013-10-21T09:45:17.273 回答