简体   繁体   中英

sprintf in PHP push notification

Im using PHP to send notificationa to subscribed users when certain values are present in a database -

The Format I'm using is

'body' => sprintf(trim($event['deviceName']) . ' went to ' . trim($event['deviceStatus']) . ' mode %s' .trim($event['deviceMessage']), date('H:i')),

And the result:

Alarm One went to Activated mode 15:30 User Set

I have tried %c \\n \\v \\r in multiple variations and positions - using double quotes etc - I can't seem to format the message to display as:

Alarm One went to Activated Mode at 15:30
User Set

with the 'deviceMessage' on a new line - on Windows using the \\n puts it on a new line but displays \\n in the push notification - on Android it just adds \\n to the string

Using external variables inside the format parameter of sprintf is a very bad idea . Rewrite your code like this, it contains \\r\\n inside double quotes:

sprintf("%s went to %s mode %s\r\n%s",
    trim($event['deviceName']),
    trim($event['deviceStatus']),
    date('H:i'),
    trim($event['deviceMessage'])
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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