简体   繁体   English

通知的待定​​意图不起作用

[英]pending intent on notification not working

I'm trying to setup a heads up notification that has a button, when the button is clicked a sms will be sent in the background, or if the actual notification is clicked it will open up an activity(this part i can do, no problem). 我正在尝试设置一个带有按钮的抬头通知,当单击该按钮时,将在后台发送短信,或者如果单击了实际通知,则会打开一个活动(我可以做的这部分,没有问题)。 this is my code below but not sure if im doing it the right way. 这是我下面的代码,但不确定即时通讯是否以正确的方式进行。

MainActivity : MainActivity

private void sendNotifaction(){

    // Create an explicit content Intent that starts the main Activity.
    Intent notificationIntent = new Intent(this, sendSmsService.class);

    // Construct a task stack.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

    // Add the main Activity to the task stack as the parent.
    stackBuilder.addParentStack(MainActivity.class);

    // Push the content Intent onto the stack.
    stackBuilder.addNextIntent(notificationIntent);

    // Get a PendingIntent containing the entire back stack.
    PendingIntent notificationPendingIntent =
           stackBuilder.getPendingIntent(1, PendingIntent.FLAG_UPDATE_CURRENT);


    // Get a notification builder that's compatible with platform versions >= 4
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    // Define the notification settings.
    builder.setSmallIcon(R.mipmap.ic_launcher)
            // In a real app, you may want to use a library like Volley
            // to decode the Bitmap.
            .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                    R.mipmap.ic_launcher))
            .setColor(Color.RED)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setVibrate(new long[]{10,10,10})
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setContentTitle("Noti")
            .setContentText(getString(R.string.geofence_transition_notification_text))
            .addAction(R.mipmap.ic_launcher,"Send",notificationPendingIntent)

            .setContentIntent(notificationPendingIntent);


    // Dismiss notification once the user touches it.
    builder.setAutoCancel(true);

    // Get an instance of the Notification manager
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Issue the notification
    mNotificationManager.notify(0, builder.build());
}

Its the intentService sendSmsService is where i think im going wrong. 它的intentService sendSmsService是我认为我出错的地方。

sendSmsService sendSmsService

public class sendSmsService extends IntentService {


String senderNum = "";
String sms = "";

/**
 * Creates an IntentService.  Invoked by your subclass's constructor.
 *
 * @param name Used to name the worker thread, important only for debugging.
 */
public sendSmsService(String name) {
    super(name);
}

@Override
protected void onHandleIntent(Intent intent) {

    try {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(senderNum, null, sms, null, null);

        Toast.makeText(getApplicationContext(), "Sms Sent", Toast.LENGTH_LONG).show();

    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), "Sms Failed", Toast.LENGTH_LONG).show();
    }

}

} }

it doesn't not seem to firing the code in the on handle at all. 它似乎根本没有触发on句柄中的代码。

I got it! 我知道了! :) after reading this page from Vogella on Broadcast receivers http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html I changed the second to class to a Broadcast receiver. :)从Vogella上广播接收机阅读此页后http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html我改变了第二类的广播接收器。 and made the relevant changes in the notification method. 并对通知方法进行了相应的更改。

sendSmsReceiver sendSmsReceiver

public class sendSmsReceiver extends BroadcastReceiver{


String senderNum = "";
String sms = "";


@Override
public void onReceive(Context context, Intent intent) {
    try {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(senderNum, null, sms, null, null);

        Toast.makeText(context, "Sms Sent", Toast.LENGTH_LONG).show();

    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(context, "Sms Failed", Toast.LENGTH_LONG).show();
    }
}

} }

MainActivity 主要活动

private void headsUpSmsNotifaction(String notificationDetails){

    // Create an explicit content Intent that starts the main Activity.
    Intent notificationIntent = new Intent(this, sendSmsReceiver.class);
    PendingIntent notificationPendingIntent = PendingIntent.getBroadcast(this,44,notificationIntent,0);


    // Get a notification builder that's compatible with platform versions >= 4
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    // Define the notification settings.
    builder.setSmallIcon(R.mipmap.ic_launcher)
            // In a real app, you may want to use a library like Volley
            // to decode the Bitmap.
            .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                    R.mipmap.ic_launcher))
            .setColor(Color.RED)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setVibrate(new long[]{10,10,10})
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setContentTitle(notificationDetails)
            .setContentText(getString(R.string.geofence_transition_notification_text))
            .addAction(R.mipmap.ic_launcher,"Send",notificationPendingIntent)

            .setContentIntent(notificationPendingIntent);


    // Dismiss notification once the user touches it.
    builder.setAutoCancel(true);

    // Get an instance of the Notification manager
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Issue the notification
    mNotificationManager.notify(0, builder.build());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM